c++ - 如何在 VC++ 2010 中使用带有 boost::bind/std::bind 的 lambda 函数?

标签 c++ visual-studio-2010 lambda c++11

我有一些想要使用 boost::bind 或 std::bind 绑定(bind)的 lambda 函数。 (不管是哪一个,只要它有效。)不幸的是,它们都给了我不同的编译器错误:

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>'

那么,最简​​单的解决方法是什么?

最佳答案

需要手动指定返回类型:

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

如果您不喜欢明确地告诉绑定(bind),您也可以自己编写一个模板函数来自动推断返回类型,使用 Boost.FunctionTypes 来检查 lambda 的 operator()。

关于c++ - 如何在 VC++ 2010 中使用带有 boost::bind/std::bind 的 lambda 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4602568/

相关文章:

c++ - 使用逻辑或 ||合并两个整数?

c++ - 类型 'wxMenuBar'必须实现继承的纯虚方法 'wxMenuBarBase::GetLabelTop'

c++ - C++ 结构中的结构

linq - 组合多个表达式 (Expression<Func<T,bool>>) 不适用于变量。为什么?

c++ - QFileDialog 的多个实例的历史

c++ - OpenGL ES 3.1 - 无法使用 glTexImage2D 创建不可变纹理

c++ - Visual Studio 命令提示符

c++ - 删除文本文件中两个特定字符之间的内容

c# - 如何在没有主键的情况下删除重复项(不同值)

entity-framework - 如何动态绑定(bind) Entity Framework 中替换表达式节点中的参数