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 (eg 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!
您提供的是EventHandler<_Sig>&&
而不是const EventHandler<_Sig>&
,因此选择了非专用版本。
To properly specialize, just use
template<>
timed_function<_Sig>& operator+=<const EventHandler<_Sig>>...
That is, remove the reference from template specialization. Here is an example of how this sort of specialization should work - with cooked-up types, but should be useful:
http://coliru.stacked-crooked.com/a/297df51929329484
(depending on your compiler, you might have to put a space between '>>' in specialization).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.