Search code examples
c++boostboost-bindboost-function

How to get the arguments binded into boost::function?


From the boost::bind docs( http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html#with_functions ), "The arguments that bind takes are copied and held internally by the returned function object", but if there's a way I could get the arguments copied in those function object?

i.e.:

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>

using namespace std;

void doSomthing(std::string str)
{
}

int main()
{    
    boost::function<void(void)> func_obj = boost::bind(&doSomthing, "some string");
    //how can I get the std::string argument("some string") through func_obj?
}

thanks in advance.


Solution

  • There's not really much you can do with a Boost.Function object, except call it - and that's by design. (You can copy it, destroy it, compare to NULL, but not much more).

    Consider the following code:

    void Foo () {}
    void Bar ( int i ) { printf ( "%d", i ); }
    
    boost::function<void(void)> fFoo (Foo);
    boost::function<void(void)> fBar = boost::bind (Bar, 23);
    

    These two objects are designed to be treated identically. They are the same type, and behave the same. There's no mechanism in boost function for distinguishing between them.

    For a great description of the techniques used in Boost.Function (and other places), check out Nevin Liber's type erasure talk from Boostcon 2010