I'm creating a generic C++ EventEmitter
. It's based on Node.js EventEmitter:
template <typename ...Args>
int16_t EventEmitter::addListener(uint32_t eventId, std::function<void(Args...)> cb)
{
...
}
template <typename... Args>
void EventEmitter::emit(uint32_t eventId, Args... args)
{
...
}
It's working as expected (I can register listeners with different prototypes). Eg.:
auto handler = [](int n) { ... };
listener.addListener(0, std::function<void(int)>(handler));
But I don't want to bother typing the whole listener prototype to std::function<...>
every time I add one (some have more than 5 parameters), then I decided to create a macro:
#define STDFUNC(fn) std::function<decltype(fn)>(fn)
The problem is when I try to use it with lambdas: decltype(handler)
is not void(int)
, it's class lambda []void (int n)->void
instead, generating the error:
(Clang 3.7.1) -> error : implicit instantiation of undefined template 'std::_Get_function_impl<(lambda at ...
I'm scratching my head to get the prototype without that lambda qualifiers but I'm stuck. Any help will be appreciated.