gpt4 book ai didi

c++ - boost::bind 返回一个函数对象,它是需要指针的函数的参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:40:33 24 4
gpt4 key购买 nike

我正在 Linux 上编写有关 boost::bind 的 C++ 代码。

boost::bind 的返回数据类型是一个函数对象,它是另一个函数bridge_set_pound_var_func 的输入参数。

但是,bridge_set_pound_var_func 的输入参数必须是一个函数指针。 bridge_set_pound_var_func 的接口(interface)不能改变。

代码如下:

#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*)

任何帮助将不胜感激。

并且,bridge_set_pound_var_func 的接口(interface)不能更改,因为它需要被许多其他函数调用。

这是有效的新代码。但是,“myC::func is called”没有打印出来,为什么?

#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 的接口(interface),它被许多其他函数调用。是否可以将 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( &pound_func_with_functor, &fn );
}

关于c++ - boost::bind 返回一个函数对象,它是需要指针的函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12432556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com