Ben Crowhurst
Ben Crowhurst

Reputation: 8481

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

I'm attempting to pass an 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 everyone's 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());

Upvotes: 9

Views: 17774

Answers (3)

Michael Price
Michael Price

Reputation: 8968

class dispatch
{
  public:
     template <typename ... Params>
     deliver( std::function<void (Params && p...)> task, Params && p )
     {
         task(std::forward(p)...);
     }
};

I haven't compiled this (corrections welcome!), but the idea should work.

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

The one thing I'm unclear about is how this behaves if Object::method has defaulted params instead of overloads.

Upvotes: 2

mloskot
mloskot

Reputation: 38872

Yes, it is possible as long as you use bind to generate compatible function objects:

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

typedef boost::function<void(void)> fun_t;
typedef std::list<fun_t> funs_t;

void foo()
    { std::cout <<"\n"; }
void bar(int p)
    { std::cout<<"("<<p<<")\n"; }
void goo(std::string const& p)
    { std::cout<<"("<<p<<")\n"; }

void caller(fun_t f)
    { f(); }

int main()
{
    funs_t f;
    f.push_front(boost::bind(foo));
    f.push_front(boost::bind(bar, int(17)));
    f.push_front(boost::bind(goo, "I am goo"));

    for (funs_t::iterator it = f.begin(); it != f.end(); ++it)
    {
        caller(*it);
    }

    return 0;
}

(Note, I use Boost.Function and Boost.Bind, but there should be no difference to use of std::bind and std::function.)

Upvotes: 3

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

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.

Upvotes: 10

Related Questions