有这样的代码
#include <iostream>
using namespace std;
int main()
{
cout<<[](){ return 0;};
cout<<[](){ return 3.2;};
cout<<[](){ return true;};
cout<<[](){ return false;};
cout<<[](){ return "Hello world!";};
cout<<[]()->int{ return 0;};
cout<<[]()->double{ return 3.2;};
cout<<[]()->bool{ return true;};
cout<<[]()->bool{ return false;};
cout<<[]()->const char*{ return "Hello world!";};
return 0;
}
编译它gcc version 4.8.2
我的输出只是1111111111
。
为什么只有“1”?
当 lambda 表达式没有捕获时,它可以隐式转换为函数指针。
函数指针又可以隐式转换为bool
,产生true
如果指针不为空,则打印该指针。
If you cout << std::boolalpha
在你的输出之前,你会看到的truetruetrue....改为打印 http://coliru.stacked-crooked.com/a/de3234b85686a77d.
如果您在 lambda 中捕获某些内容,则它不再可转换为函数指针,并且你会得到一个编译器错误 http://coliru.stacked-crooked.com/a/135a3369b2cee9c7.
如果你想打印调用 lambda 返回的结果,那么你需要()
正如其他人指出的那样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)