I have 2 functions inside a class, one is the default specilization of the operator+= which expects a function of some sort, whilst the second specilization expects an EventHandler, here is the implementation:
template<typename _Func>
timed_function<_Sig>& operator+=( _Func &&f )
{
// Create an unamed handler
auto handler = new EventHandler<_Sig>( "unamed", std::forward<_Func>( f ) );
// Push it
_fs.push_back( handler );
// Return a reference to the function
return handler->get( );
}
Here's the specialized version:
template<>
timed_function<_Sig>& operator+=<const EventHandler<_Sig>&>( const EventHandler<_Sig> &_Handler )
{
// Copy the handler and push it
_fs.push_back( new EventHandler<_Sig>( _Handler ) );
// Return a reference to the function
return _fs.back( )->get( );
}
Where _fs
is just a vector of EventHandler<_Sig>
Pointers.
And _Sig
being the function signature (e.g void(int)
)
When using the += operator on for example a lambda function it works just fine and the compiler picks the correct specialization:
window->OnKeyDown( ) += []( dx::Window *sender, dx::KeyDownArgs &args )
{
[...]
};
OnKeyDown( )
returns a reference to an instance of Event<void(dx::Window*, dx::KeyDownArgs&)>
However when I try and manually add an EventHandler like this it still picks the non-specialized version of the function.
window->OnKeyDown( ) += EventHandler<void(dx::Window*, dx::KeyDownArgs&)>( "Key Handler",
[]( dx::Window *sender, dx::KeyDownArgs &args )
{
[...]
} );
Thanks!