本文介绍了lambda表达式可以作为函数指针传递吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图传递一个lambda表达式到一个函数接受一个函数指针,这是可能吗?
这里是一些示例代码,我使用VS2010:
#include< iostream> using namespace std; void func(int i){cout< 我将被召唤:< i <_ endl;} void fptrfunc(void(* fptr)(int i),int j){fptr(j);} int main ){ fptrfunc(func,10); // this is ok fptrfunc([](int i){cout<LAMBDA CALL<< i<< endl;},20) // Does NOT COMPILE return 0;在VC10 RTM中,没有 - - - - - - - - - - - - - - - - - - - - - - - - - - 但是在VC10中的lambda特性最终确定之后,标准委员会添加了允许无状态lambdas降级为函数指针的语言。所以在未来这将是可能的。I am trying to pass a lambda expression to a function that takes a function pointer, is this even possible?
Here is some sample code, I'm using VS2010:
#include <iostream> using namespace std; void func(int i){cout << "I'V BEEN CALLED: " << i <<endl;} void fptrfunc(void (*fptr)(int i), int j){fptr(j);} int main(){ fptrfunc(func,10); //this is ok fptrfunc([](int i){cout << "LAMBDA CALL " << i << endl; }, 20); //DOES NOT COMPILE return 0; }解决方案
In VC10 RTM, no - but after the lambda feature in VC10 was finalized, the standard committee did add language which allows stateless lambdas to degrade to function pointers. So in the future this will be possible.
更多推荐
lambda表达式可以作为函数指针传递吗?
发布评论