struct A
{
A(int v):value(v){}
int someFun(){return value;}
int someOtherFun(int v=0){return v+value;}
int value;
};
int main()
{
boost::shared_ptr<A> a(new A(42));
//boost::function<int()> b1(bind(&A::someOtherFun,a,_1)); //Error
boost::function<int()> b2(bind(&A::someFun,a));
b2();
return 0;
}
bind(&A::someOtherFun,a)();
失败并出现编译错误:错误:非静态成员函数的使用无效
如何绑定(bind)类似于someFun的someOtherFun?即,它们应该绑定(bind)到相同的 boost::function 类型。
最佳答案
A::someFun()
和 A::someOtherFun()
有不同的类型:第一个不需要参数,第二个需要 1(可以省略和编译器为你插入默认值)
尝试:
bind(&A::someOtherFun, a, _1)(1);
问题是,当您通过 bind()
调用该函数时,编译器不知道该绑定(bind)函数有默认参数值,因此给您错误,因为您没有所需参数
关于c++ - boost::bind 为具有默认值的成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10931846/