如何使用lambda函数与boost :: bind / std :: bind在VC ++ 2010? [英] How to use lambda functions with boost::bind/std::bind in VC++ 2010?

查看:465
本文介绍了如何使用lambda函数与boost :: bind / std :: bind在VC ++ 2010?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些lambda函数,我想使用boost :: bind或std :: bind绑定。不幸的是,他们给我不同的编译器错误:

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?

推荐答案

您需要手动指定返回类型:

You need to manually specify the return type:

boost::bind<void>(f)();
boost::bind<int>(f2, 13)();

你也可以自己写一个模板函数来自动推导出使用Boost.FunctionTypes检查的返回类型你的lambda的operator(),如果你不喜欢显式地告诉bind。

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.

这篇关于如何使用lambda函数与boost :: bind / std :: bind在VC ++ 2010?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆