提高::绑定与默认值的成员函数? [英] boost::bind for a member function with default value?

查看:174
本文介绍了提高::绑定与默认值的成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
}

绑定(安培; A :: someOtherFun,A)(); 失败,编译错误:错误:无效使用非静态成员函数

如何绑定someOtherFun类似someFun?即,应结合相同的boost ::功能类型。

How to bind someOtherFun similar to the someFun? i.e, they should bind to the same boost::function type.

推荐答案

A :: someFun() A :: someOtherFun()有不同的类型:第一希望没有参数,第二个预计1(可ommitted和编译器插入defaqult值你)

A::someFun() and A::someOtherFun() have different types: the first expects no parameters, the second expects 1 (which can be ommitted and the compiler inserts the defaqult value for you)

尝试:

bind(&A::someOtherFun, a, _1)(1);

问题是,当你调用通过功能绑定(),编译器不知道有该绑定功能的默认参数值,从而为您提供了错误因为你不具备所需的参数

The problem is that when you call the function via bind(), the compiler does not know there is a default parameter value for that bound function and thus gives you error because you don't have the required parameter

这篇关于提高::绑定与默认值的成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆