dimanche 4 janvier 2015

Overload depending on result of lambda

I have a class like:



class foo
{
EventManager<LRESULT(UINT, WPARAM, LPARAM)> Events;

template<typename T>
void addListener(int e, T&& lambda) { events.add(e, lambda); }
};


However, I need to accept TWO types of lambdas. One with the signature:


[&](UINT, WPARAM, LPARAM) -> LRESULT {} and one with the signature: [&](UINT, WPARAM, LPARAM) -> void {}.


So I want to determine the return type of the lambdas..


I tried something like:



template<typename T>
void addListener(int e, T&& listener)
{
if (std::is_void<decltype(listener(0, 0, 0))>::value)
{
Events.Subscribe(e, [&](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
listener(msg, wp, lp);
return DefWindowProcW(this->Handle(), msg, wp, lp);
});
}

Events.Subscribe(e, [&](UINT msg, WPARAM wp, LPARAM lp) -> LRESULT {
std::function<LRESULT()> func = std::bind(listener, msg, wp, lp);
return func();
});
}


I also tried declaring the function as a template with the following signature:


template<typename T, typename = typename std::enable_if<std::is_void<typename std::result_of<T(UINT, WPARAM, LPARAM)>::type>::value>::type>


to overload it but it doesn't work either..


Any ideas what I can do without having to create two methods (one for each signature)?


Aucun commentaire:

Enregistrer un commentaire