pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何使用模板將 lambda 轉換為 std::function

How to convert a lambda to an std::function using templates(如何使用模板將 lambda 轉換為 std::function)
本文介紹了如何使用模板將 lambda 轉換為 std::function的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

基本上,我希望能夠做的是使用任意數量的任意類型參數的 lambda 并將其轉換為 std::function.我嘗試了以下方法,但都不起作用.

Basically, what I want to be able to do is take a lambda with any number of any type of parameters and convert it to an std::function. I've tried the following and neither method works.

std::function([](){});//Complains that std::function is missing template parameters
template <typename T> void foo(function<T> f){}
foo([](){});//Complains that it cannot find a matching candidate

然而,以下代碼確實有效,但這不是我想要的,因為它需要明確聲明模板參數,這不適用于通用代碼.

The following code does work however, but it is not what I want because it requires explicitly stating the template parameters which does not work for generic code.

std::function<void()>([](){});

我整個晚上都在處理函數和模板,但我無法弄清楚,因此非常感謝您的幫助.

I've been mucking around with functions and templates all evening and I just can't figure this out, so any help would be much appreciated.

正如評論中提到的,我嘗試這樣做的原因是因為我試圖使用可變參數模板在 C++ 中實現柯里化.不幸的是,這在使用 lambda 時會失敗.例如,我可以使用函數指針傳遞一個標準函數.

As mentioned in a comment, the reason I'm trying to do this is because I'm trying to implement currying in C++ using variadic templates. Unfortunately, this fails horribly when using lambdas. For example, I can pass a standard function using a function pointer.

template <typename R, typename...A>
void foo(R (*f)(A...)) {}
void bar() {}
int main() {
    foo(bar);
}

但是,我不知道如何將 lambda 傳遞給這樣的可變參數函數.為什么我對將泛型 lambda 轉換為 std::function 感興趣是因為我可以執行以下操作,但最終要求我將模板參數顯式聲明為 std::function,這是我試圖避免的.

However, I can't figure out how to pass a lambda to such a variadic function. Why I'm interested in converting a generic lambda into an std::function is because I can do the following, but it ends up requiring that I explicitly state the template parameters to std::function which is what I am trying to avoid.

template <typename R, typename...A>
void foo(std::function<R(A...)>) {}
int main() {
    foo(std::function<void()>([](){}));
}

推薦答案

你不能在沒有明確指定模板的情況下將 lambda 函數對象作為 std::function 類型的參數傳遞參數 T.模板類型推導嘗試將您的 lambda 函數的類型與 std::function 匹配,在這種情況下它無法做到 - 這些類型不相同.模板類型推導不考慮類型之間的轉換.

You can't pass a lambda function object as an argument of type std::function<T> without explicitly specifying the template argument T. Template type deduction tries to match the type of your lambda function to the std::function<T> which it just can't do in this case - these types are not the same. Template type deduction doesn't consider conversions between types.

如果你能給它一些其他的方式來推斷類型,這是可能的.您可以通過將函數參數包裝在 identity 類型中來做到這一點,這樣它就不會在嘗試將 lambda 與 std::function 匹配時失敗(因為依賴類型是只是被類型推導忽略了)并給出了一些其他參數.

It is possible if you can give it some other way to deduce the type. You can do this by wrapping the function argument in an identity type so that it doesn't fail on trying to match the lambda to std::function (because dependent types are just ignored by type deduction) and giving some other arguments.

template <typename T>
struct identity
{
  typedef T type;
};

template <typename... T>
void func(typename identity<std::function<void(T...)>>::type f, T... values) {
  f(values...);
}

int main() {
  func([](int x, int y, int z) { std::cout << (x*y*z) << std::endl; }, 3, 6, 8);
  return 0;
}

這顯然在您的情況下沒有用,因為您不想在以后傳遞值.

This is obviously not useful in your situation though because you don't want to pass the values until later.

由于您不想指定模板參數,也不想傳遞可以從中推斷出模板參數的其他參數,因此編譯器將無法推斷出您的 std 的類型::function 參數.

Since you don't want to specify the template parameters, nor do you want to pass other arguments from which the template parameters can be deduced, the compiler won't be able to deduce the type of your std::function argument.

這篇關于如何使用模板將 lambda 轉換為 std::function的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 细石混凝土泵_厂家_价格-烟台九达机械有限公司 | 污水处理设备-海普欧环保集团有限公司 | 天然气分析仪-液化气二甲醚分析仪|传昊仪器 | 高硼硅玻璃|水位计玻璃板|光学三棱镜-邯郸奥维玻璃科技有限公司 高温高压釜(氢化反应釜)百科 | 缝纫客| 科研ELISA试剂盒,酶联免疫检测试剂盒,昆虫_植物ELISA酶免试剂盒-上海仁捷生物科技有限公司 | 据信,上课带着跳 D 体验-别样的课堂刺激感受引发网友热议 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 颗粒机,颗粒机组,木屑颗粒机-济南劲能机械有限公司 | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 打孔器,打孔钳厂家【温州新星德牌五金工具】 | 塑料薄膜_PP薄膜_聚乙烯薄膜-常州市鑫美新材料包装厂 | 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 杭州ROHS检测仪-XRF测试仪价格-百科 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 餐饮小吃技术培训-火锅串串香培训「何小胖培训」_成都点石成金[官网] | 立刷【微电签pos机】-嘉联支付立刷运营中心 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | 木材烘干机,木炭烘干机,纸管/佛香烘干设备-河南蓝天机械制造有限公司 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 定制/定做衬衫厂家/公司-衬衫订做/订制价格/费用-北京圣达信 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 诺冠气动元件,诺冠电磁阀,海隆防爆阀,norgren气缸-山东锦隆自动化科技有限公司 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 广东之窗网 | 环比机械| 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 | 苏州同创电子有限公司 - 四探针测试仪源头厂家 | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 潜水搅拌机-双曲面搅拌机-潜水推进器|奥伯尔环保 |