我std::bind在代码的各个地方使用时遇到了很多麻烦.有时候它会起作用,有时却不起作用,所以我认为我做的事情根本就是错误的.
据我了解,以下基本用法std::bind应该可以正常工作:
#include <functional>
int foo(int a, int b){ return a+b; }
int main(){
using namespace std::placeholders;
// works
auto bar_auto=std::bind(foo,1,_2);
// compile error
std::function<int(int)> bar_fun=std::bind(foo,1,_2);
int quux=1;
// compile error
std::function<int(int)> bar_fun_lvalue=std::bind(foo,quux,_2);
}
Run Code Online (Sandbox Code Playgroud)
毫无疑问的类型bar_auto是std::function<int(int)>(类型foo1个int参数绑定),所以为什么bar_fun编译失败?我包括bar_fun_lvalue因为一些谷歌搜索向我显示rvalues曾经是有问题的.但这并没有解决任何问题.
它类似于这个bug,但是它太旧了我不认为它是相关的.
gcc的输出不是特别有启发性:
在bindnew.cpp中包含的文件中:1:0:/usr/include/c++/4.7/functional:在'static _Res std :: _ Function_handler <_Res(_ArgTypes ...),_Functor> :: _ M_invoke(const std)的实例化中:: _ Any_data&,_ ArgTypes ...)[with _Res = int; _Functor = std :: …
c++11 ×1