Boost logo

Boost Users :

Subject: [Boost-users] boost::function, pointer to a member function.
From: Mikhail Tatarnikov (mtatarnikov_at_[hidden])
Date: 2010-06-10 21:28:39


Hi guys,

Is there a way to make boost::function to be passed to a function required a
pointer to a member function as parameter:

struct CFoo
{
        int foo1(int){}
        int foo2(int, double){}
};

int g(int (CFoo::*)(int));

int main()
{
  g(&CFoo::foo1);

  g(boost::bind(&CFoo::foo2, _1, _2, 666)); // Doesn't compile -
boost::bind returns an object instead of function pointer

// The following doesn't compile - can't convert from int (*)(CFoo*, int) to
int (CFoo::*)(int)
  g(boost::function<int (CFoo*, int)>(
      boost::bind(&CFoo::foo2, _1, _2, 666)
    ).target<int (CFoo*, int)>()
  )
}

The initial problem is to bind a parameter to a member function. It's easy to
solve the problem by changing g(), e.g.:

template<typename Functor>
int g(Functor fun);

but is it possible to solve the problem is g() can't be changed?

Regards,
Mike.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net