我有一个静态函数foo,但我想调用的 API 只接受指向函子(类似接口)的指针。有没有办法传递foo给 API?或者我需要根据函子重新实现foo。
示例代码:
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;
}
我的问题是调用 API,实现Functor只是解决方案,或者有一种方法可以用来foo将其传递给API?
会boost::bind在这里帮忙吗?
我的意思是boost::bind(foo, _1)将函数对象从函数对象中取出,foo然后是否有办法从函数对象中形成所需的仿函数?