如何在 VC 中将 lambda 函数与 boost::bind/std::bind 一起使用2010年?
我有一些 lambda 函数,我想使用 boost::bind 或 std::bind 进行绑定。 (不在乎哪一个,只要它有效即可。)不幸的是,它们都给了我不同的编译器错误:
auto f = [](){ cout<<"f()"<<endl; };
auto f2 = [](int x){ cout<<"f2() x="<<x<<endl; };
std::bind(f)(); //ok
std::bind(f2, 13)(); //error C2903: 'result' : symbol is neither a class template nor a function template
boost::bind(f)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda0>'
boost::bind(f2, 13)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda1>'
那么,最简单的解决方法是什么?
I have some lambda functions which I want to bind using either boost::bind or std::bind. (Don't care which one, as long as it works.) Unfortunately both of them give me different compiler erros:
auto f = [](){ cout<<"f()"<<endl; };
auto f2 = [](int x){ cout<<"f2() x="<<x<<endl; };
std::bind(f)(); //ok
std::bind(f2, 13)(); //error C2903: 'result' : symbol is neither a class template nor a function template
boost::bind(f)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda0>'
boost::bind(f2, 13)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda1>'
So, what is the simplest workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要手动指定返回类型:
如果您不喜欢显式告诉bind,您还可以自己编写一个模板函数,使用Boost.FunctionTypes 自动推导返回类型来检查您的lambda 的operator()。
You need to manually specify the return type:
You can also write yourself a template-function to deduce the return type automagically using Boost.FunctionTypes to inspect your lambda's operator(), if you don't like to explicitly tell bind.
我想您可能对 此 MSDN 感兴趣论坛帖子。
听起来发帖者遇到了与您相同的问题,并在 MS Connect 中提出了错误。
I think you might be interested in this MSDN forum post.
Sounds like the poster had the same problem as yours, and lodged a bug with MS Connect.