after saw Unity's delegates and events, i'm trying to code my own: I want to create a class with a variadic template, to specify the return type of the functions, and optionals arguments.
template <typename Ret, typename... Args>
class MyDelegate{
std::vector<Ret(*)(Args...)> vec;
public:
void operator+=( const Ret(*)(Args...)& newElement)
{
vec.push_back(newElement);
}
Ret operator()(const Args&... args)
{
for (auto i = vec.begin(); i != vec.end(); ++i)
(*i)(args...);
}
};
As you can see, i would like this class to be used this way:
MyDelegate<void> voidDelegate;
MyDelegate<void, int> intDelegate;
MyDelegate<int, char, boolt> miscDelegate;
and "adding" functions to each one using += operators, like:
voidDelegate += voidFunc;
//etc...
I'm having problem with the += operator for now, because VS don't accept this:
MyDelegate<void, int> delegate1;
delegate1 += [](const int a)->void{std::cout << a << std::endl; };
The lambda function is correct: it take an int and return void, so i don't understand whats' wrong.