I just found myself creating a class
template <typename T> struct invoker {
void operator()(T& it) const {it();}
};
so I could pass an invoker<foo>
to something (which isn't under my control) which wants to call invoker<foo>::operator()(foo&)
on it repeatedly with different foo
instances, to get it to forward those calls to the foo
's foo::operator()()
method.
I know it's only a few lines, but this seems like the sort of thing which is probably already provided for by STL's functional, or boost::bind
somehow. Except I can't see the trick, if there is one. (I'm sure I'm not the first person to use something very like this; does it have a name ?)
foo::operator()
directly? I mean, the class which callsinvoker::operator()
has to do something likesome_invoker(some_foo);
, while you could just call it like this:some_foo();
. You're adding an unnecessary layer of indirection. – Lariosbind
or lambdas making this any better. – ThorvaldsenT
as the function type. – Dismukestd::vector<std::function<void()>>
and callin them all, right? – Dismuke