boost::function, pointer to a member function.

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

Mikhail Tatarnikov wrote:
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
There is no way to fix this. g expects a pointer to member function of the class CFoo.
// The following doesn't compile - can't convert from int (*)(CFoo*, int) to int (CFoo::*)(int) g(boost::function
( boost::bind(&CFoo::foo2, _1, _2, 666) ).target () ) }
I believe there are some compilers where you can do evil ugly hacks so that you can make a function taking a pointer to an object and n other arguments pretend to be a member function taking n arguments. Otherwise there is no solution but to change g.

AMDG Mikhail Tatarnikov wrote:
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));
<snip>
but is it possible to solve the problem is g() can't be changed?
No. Sorry. The only way to solve this without changing g is to add another member function to CFoo which forwards to a global boost::function. (Ugh.) In Christ, Steven Watanabe
participants (3)
-
Mathias Gaunard
-
Mikhail Tatarnikov
-
Steven Watanabe