的代码
我已经看过关于boost::bind的返回类型是什么?看,简短的答案是你不需要知道。
虽然我有一个以"bind(…)result"为参数的函数,但我发现以下差异行为
工作案例1
void func(int a){};
myfunc(bind(&obj::func,this,_1));
不适用情况2(当我想用2个参数绑定func2时)
void func2(int a, int b){};
myfunc(bind(&obj::func2,this,_1,_2));
用case 3
void func3(int a, int b){};
myfunc(bind(&obj::func3,this,_1, 10));
所以我的问题是下面3的返回类型有什么不同?
bind(&obj::func,this,_1));
bind(&obj::func2,this,_1,10)); //why this one can be passed the same type as above one?
bind(&obj::func3,this,_1,_2));
由于myfunc
是相当嵌入在模板和重载函数中,我还没有找到它是如何定义的,以"bind(…)"作为参数。这就是为什么我没有附加myfunc
正如您链接的答案所示,您实际上不需要知道确切的返回类型。然而,该类型与boost::function
兼容,因此您可以这样做(作为void(void)
的最简单示例):
typedef boost::function<void(void)> myFunctionType;
// this is the function that you want to bind
void foo(){}
// this is the function that should take it as an argument
void myOtherFunction(myFunctionType f);
<...>
// this is how you bind it
myFunctionType bar = boost::bind(foo);
// and this is how you pass it as argument
myOtherFunction(bar);