Consider the following setup:
I am given an interface
template<class T>
void FooClass<T>::foo(boost::function<double (int)> f)
{...}
I want to implement f using a Functor:
class MyFun {
public: double operator()(int a) {do something...;}
}
However there is another function defined in the interface
template<class T>
template <class FunPtr>
void FooClass<T>::foo(const FunPtr& f)
{...}
When a FooClass object is called,
MyFun f;
FooClass<double> fooclass;
fooclass.foo(f);
it uses the second definition, while I want it to call the first one – can this be changed somehow?
The compiler ‘preferres’ the second definition because the template version results in an exact match for the function object parameter, whereas the
boost::functionparameter needs an implicit conversion to be accepted (and implicit conversions are considered after direct overload resolution). You can achieve what you want just by constructing aboost::functionobject, and then passing it to the function (note that you can do this in the same line, I’m just doing it separately for clarity):