I have a static function foo
but the API I want to call only accept pointer to a functor (of similar interface ). Is there a way to pass foo
to the API? or I need to re-implement foo
in terms of functor.
Example code:
template<typename ReturnType, typename ArgT>
struct Functor: public std::unary_function<ArgT,ReturnType>
{
virtual ~Functor () {}
virtual ReturnType operator()( ArgT) = 0;
};
// I have a pre written function
static int foo (int a) {
return ++a;
}
// I am not allowed to change the signature of this function :(
static void API ( Functor<int,int> * functor ) {
cout << (*functor) (5);
}
int main (void) {
API ( ??? make use of `foo` somehow ??? );
return 0;
}
My question is for calling API, implementing Functor
is only solution or there is a way by which I could use foo
to pass it to API
?
Will boost::bind
help here?
I mean boost::bind(foo, _1)
will make function object out of foo
and then if there is a way to form desired functor out of function object?