I have to pass function into pointer. For this purposes I'm using boost::function. The function which catches the pointer is overloaded for different signatures. For example:
void Foo(boost::function<int ()>) { ... }
void Foo(boost::function<float ()>) { ... }
void Foo(boost::function<double ()>) { ... }
Now I wanna pass some class-method pointer there:
class test
{
public:
float toCall() { };
};
class Wrapper
{
Wrapper() {
test obj;
Foo(boost::bind(&test::toCall, this));
}
};
error: no matching function for call to ‘Foo(boost::_bi::bind_t<float, boost::_mfi::mf0<float, test>, boost::_bi::list1<boost::_bi::value<Wrapper*> > >)’
note: candidates are: Foo(boost::function<float()>&)
Nonono this cannot work. Because boost::function<...>
has a templated constructor to accept any and all types. Compatibility with the call signature is checked later on. Overload resolution cannot resolve this.
Also, i think you want to pass &obj
instead of this
. Try converting explicitly:
Foo(boost::function<float ()>(boost::bind(&test::toCall, &obj)));
This is utterly ugly though so you may want to introduce a typedef
void Foo(FloatHandler) { ... }
...
FloatHandler f(boost::bind(&test::toCall, &obj));
Foo(f);
Or ultimately you could make Foo
a template that accepts just any callable type T
. I suspect that may be the simplest, because in the general case i suspect you don't know to what boost::function<...>
you need to cast to. And how about folks that want to return a std::complex<>
. So...
template<typename T>
void Foo(T) { ... }
...
Foo(boost::bind(&test::toCall, &obj));
Hope this helps.