我正在Linux上做关于boost :: bind的C ++编码。
boost :: bind的返回数据types是一个函数对象,它是另一个函数bridge_set_pound_var_func的input参数。
但是,bridge_set_pound_var_func的input参数必须是一个函数指针。 bridge_set_pound_var_func的接口无法更改。
代码如下:
#include <boost/bind.hpp> #include <iostream> using namespace boost; class myA { public: int bridge_set_pound_var_func( int (*pound_var_func)(const char *, char *, void *), void *arg ) { std::cout << "bridge_set_pound_func is called " << std::endl ; return 0; } }; class myC { public: myA *myOA; int func(const char * poundVar , char * t1, void * t2); int myCCall() { myOA->bridge_set_pound_func( (boost::bind(&myC::func, this)), (void *)this ); return 0;} }; int myC::func(const char * poundVar , char * t1, void * t2) { std::cout << "myC::func is called " << std::endl; return 1; } int main() { myC myCO ; myC *m1p = &myCO ; m1p->myCCall() ; return 0 ; } // EOF
我有编译错误:
error: no matching function for call to 'myA::bridge_set_pound_func(boost::_bi::bind_t<int (&)(const char*, char*, void*), boost::_mfi::dm<int ()(const char*, char*, void*), myC>, boost::_bi::list1<boost::_bi::value<myC*> > >, void*)' note: candidates are: int myA::bridge_set_pound_func(int (*)(const char*, char*, void*), void*)
任何帮助将不胜感激。
这是工作的新代码。 但是,“myC :: func被调用”不打印,为什么?
#include <boost/bind.hpp> #include <boost/function.hpp> #include <iostream> using namespace boost; class myA { public: int bridge_set_pound_var_func( const boost::function3<int, const char *, char *, void *> f, void *arg ) { std::cout << "bridge_set_pound_var_func is called " << std::endl ; return 0; } }; typedef int (*funcPtr)(const char *, char *, void *) ; typedef boost::function0<int&> boostBindFuncType; class myC { public: myA *myOA; int func(const char * poundVar , char * t1, void * t2); int myCCall() { std::cout << "myCCall is called " << std::endl; myOA->bridge_set_pound_var_func( (boost::bind(&myC::func, this, _1, _2, _3)), (void *)this ); return 0; } }; int myC::func(const char * poundVar , char * t1, void * t2) { std::cout << "myC::func is called " << std::endl; return 1; } int main() { myC myCO ; myC *m1p = &myCO ; m1p->myCCall() ; return 0 ; }
我不能改变被许多其他函数调用的bridge_set_pound_var_func的接口。 是否有可能将boost :: bind返回的函数对象转换为函数指针?
传统上,传递给回调函数的最终void *
参数是用户定义的数据。 如果这是你的情况,你可以创建一个帮助函数,让你传递函子。 唯一的麻烦是,你需要确保用户数据存在,直到回调将不再被调用 – 这将取决于你的程序结构。 我只是要泄漏对象,因为这是确保它继续存在的最简单的方法。 (虽然你也会遇到含有myC
对象的问题)。
class myC { public: myA *myOA; //NOTE: I've removed the void* ptr here, which should be user data. // If you still need it you'll need to bind it away at functor creation time. int func(const char * poundVar , char * t1); int myCCall() { //TODO: Fix this intentional leak. boost::function<int(const char*, char*)> * fn = new boost::function<int(const char*,char*)>( boost::bind(&myC::func,this) ); //Call our helper function instead of `set_pound_func`. set_pound_func_with_functor(myOA, fn ); return; } }; // Function that is really passed to `set_pound_func` when // set_pound_func_with_functor is called. // It converts the user data back to a boost function and calls it. int pound_func_with_functor(const char* d1, char* d2, void* user_data) { boost::function<int(const char*,char*)> * fn = static_cast< boost::function<int(const char*, char*) >( user_data ); return (*fn)(d1,d2); } //Helper function to make set_pound_func work with boost functions instead. //NOTE: You are required to ensure the fn argument exists for long enough... void set_pound_func_with_functor( myA * myOA, boost::function<int(const char *, char *)> & fn ) { myOA->bridge_set_pound_var_func( £_func_with_functor, &fn ); }
没有办法将boost::bind
结果转换为函数指针。
使用boost::function
int bridge_set_pound_var_func ( const boost::function<int(const char *, char *, void *)>& f, void *arg )
称为
myOA->bridge_set_pound_var_func( (boost::bind(&myC::func, this, _1, _2, _3)), (void *)this );
或使用template parameter
template<typename Func> int bridge_set_pound_var_func( const Func& f, void *arg )
在第一种情况下打电话。
您不能将boost::bind
或boost::function
结果转换为函数指针。
我认为读这将是interstring。
将boost :: function降级为普通函数指针
在你的情况 – 你不能使用target
,所以,看看伊恩的答案
唯一可以作为函数指针传递的是一个函数。 没有函数对象,没有lambda表达式,没有任何东西。 只是全球功能。