我有一些想要使用 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/