Corvusoft Corvusoft - 8 days ago 5
C++ Question

c++0x std::function as a method argument

I'm attempting to pass a std::function via a method like so:

class dispatch
{
public:
deliver( std::function<void ( void )> task );
}


This works as expected. However i wish to pass arguments to some of the methods supplied as the task but would prefer not to have to create overloads for all the different function< ... > forms.

for example is it at all possible just to create a method like follows

deliver( std::function& task );


and just invoke with

dispatch->deliver( bind( &Object::method, X ) );


or

dispatch->deliver( bind( &Object::method, X, arg1, arg2 ) );


etc...

Thanks for everyones input. It would appear my real fault is with calls to dispatch->deliver with the additional argument also being a bind call.

dispatch->deliver( bind( &Object::method1, X1, bind( &Object::method1, X2 ) );



error: /usr/include/c++/v1/functional:1539:13: error: no matching function for call to '__mu_expand'
return __mu_expand(__ti, __uj, __indices());

Answer

std::function<void(void)> is already polymorphic, that's the whole point of it. So those two last snippets will work (as long as the functor bind returns can be called with no arguments, and returns nothing, of course), without changing what you already have.