[英]boost::bind return a function object which is the argument for a function that requires pointer
我正在Linux上做关于boost :: bind的C ++编码。
boost :: bind的返回数据类型是一个函数对象,它是另一个函数bridge_set_pound_var_func的输入参数。
但是,bridge_set_pound_var_func的输入参数必须是函数指针。 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返回的函数对象转换为函数指针吗?
没有办法将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
结果转换为函数指针。
我认为阅读这将是非常有意义的。
在你的情况下 - 你不能使用target
,所以,看看伊恩的答案
传统上,传递给回调的最终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 );
}
作为函数指针唯一可以传递的是函数。 没有功能对象,没有lambdas,没有任何东西。 只是全球功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.