I have a template class Delegate, with an overloaded += operator, that makes using delegates similar to C#.
// ... generalized version of the template omitted from code
template<typename... TArgs>
class Delegate<void, TArgs...>
{
private:
using Func = std::function<void(TArgs...)>;
std::vector<Func> funcs;
public:
template<typename T> Delegate& operator+=(T mFunc) { funcs.push_back(Func(mFunc)); return *this; }
void operator()(TArgs... mParams) { for (auto& f : funcs) f(mParams...); }
};
This is what I’m trying to do:
struct s
{
void test() { }
void run()
{
Delegate<void> d;
d += [] { /* do something */ ; };
d += test; // does not compile
}
};
Is there a way to allow d += test; to work?
How can it compile? This function is supposed to return nothing. Its return type is
void.Also, I assume you have defined (or declared ) the primary template:
Also assuming that
delegateis a typo, as the class template isDelegate.Anyway, with
testreturning nothing, it compiles fine:http://stacked-crooked.com/view?id=c56b7a2e758f8fbc361228834c90822b
As for member-function-pointers, your current implementation doesn’t support it. Note that a non-static member function pointer takes the form of
R (C::*MemPtr)(Args...) cv. Just work on it.