lambda表达式可以作为函数指针传递吗?

编程入门 行业动态 更新时间:2024-10-20 20:33:59
本文介绍了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表达式可以作为函数指针传递吗?

本文发布于:2023-11-08 07:38:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   指针   函数   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!