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

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.


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


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.