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());
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With