1
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)(); fails with compile error: error: invalid use of non-static member function

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

balki
  • 26,394
  • 30
  • 105
  • 151

1 Answers1

2

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)

Try:

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

Attila
  • 28,265
  • 3
  • 46
  • 55
  • @balki - I forgot to add the placeholder for the parameter. See edited version – Attila Jun 07 '12 at 13:07
  • Yes, this works. But What I am trying to do this something like this `boost::function b1(bind(&A::someOtherFun,a,_1));boost::function b2(bind(&A::someFun,a));` – balki Jun 07 '12 at 13:37
  • @balki - as I mentioned, the type of `A::someOtherFun` is such that it _requires_ an `int` parameter (which the compiler can supply in normal uses, but not when used in binding). So you either need `boost::function b2(bind(&A::sumeFun, a, _1));` or you need to bind a specific parameter (e.g. the default one?) if you want to be able to call it with no parameters: `boost::function b2(bind(&A::sumeFun, a, 0));` see [here(ideone.com)](http://ideone.com/VfF3V) for a compiling code – Attila Jun 07 '12 at 13:45