Search code examples
c++cloneboost-function

cloning a boost::function into a pointer and calling the wrapped function with that pointer


I am trying to create a copy of a boost::function by using a pointer and call that function using that pointer. My questions are

  1. cloning a boost::function that way is something correct
  2. the call to fp->target() should call or not the function wrapped by the boost::function?

Thanks a lot

  boost::function<void()> f = boost::bind(&my_f,my_value);
  boost::function<void()> fp* = new boost::function<void()>( f ); // clone f

  typedef void(*fptr_type)();
  fp->target<fptr_type>();  // doesn't work! Is this correct?

  fp->operator(); // doesn't compile
                  //=>error: statement cannot resolve address of overloaded function

Solution

  • If boost::function provides a copy constructor, you can assume that it will work and take care of all the lifetime issues of f in this case ( otherwise it wouldn't be provided, and you should file a bug report on their bugzilla ).

    fp->operator();
    

    Is just the function, what you're meaning to do is:

    fp->operator()();
    

    Or as above poster:

    (*fp)();