boost::bind return a function object which is the

2019-08-08 02:09发布

I am doing C++ coding on Linux about boost::bind.

The return data type of boost::bind is a function object, which is an input argument to another function bridge_set_pound_var_func.

But, bridge_set_pound_var_func's input argument must be a function pointer. bridge_set_pound_var_func's interface cannot be changed.

The code is as follows:

#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

I got compile error:

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

Any help will be appreciated.

And, the interface of bridge_set_pound_var_func cannot be changed because it needs to be called by many other functions.

This is the new code that work. But, "myC::func is called" is not printed, why ?

#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 ;
 }

I cannot change the interface of bridge_set_pound_var_func, which is called by many other functions. Is is possible to transform boost::bind returned function object to a function pointer?

3条回答
你好瞎i
2楼-- · 2019-08-08 02:43

There are no ways to convert boost::bind result to function pointer.

Use boost::function

int bridge_set_pound_var_func
( const boost::function<int(const char *, char *, void *)>& f, void *arg )

call as

myOA->bridge_set_pound_var_func( (boost::bind(&myC::func, this, _1, _2, _3)),
(void *)this );

or use template parameter

template<typename Func>
int bridge_set_pound_var_func( const Func& f, void *arg )

call as in first case.

You cannot convert result of boost::bind or boost::function to function-pointer.

I think read this will be interestring.

demote boost::function to a plain function pointer

In your case - you cannot use target, so, look at answer of Ian

查看更多
做个烂人
3楼-- · 2019-08-08 02:44

Traditionally the final void * argument passed to callbacks is user defined data. If this is the case for you you can create a helper function that will let you pass functors. the only trouble is that you need to ensure that the user data exists until the callback will no longer be called - and that will depend a lot on your program structure. I'm just going to leak the object as thats the easiest way to ensure it continues to exist. ( Though you'll also have problems with the existance of the containing myC object ).

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 );
}
查看更多
混吃等死
4楼-- · 2019-08-08 02:45

The only thing you can pass as a function pointer is a function. No function objects, no lambdas, no nothing. Just global functions.

查看更多
登录 后发表回答