I have an event source base class, which defines a function to add listeners, and I'm trying to use a template argument to resolve ambiguity. But that doesn't seem to work - it always uses the function from the first base class and then complains that template parameters don't match:
#include <type_traits>
template<class Event>
class EventSource
{
public:
class Listener
{
public:
virtual void handle( const Event& e ) = 0;
};
template<class EventType>
typename std::enable_if<std::is_same<Event, EventType>::value, void>::type
addListener( Listener* ptr );
};
class Event1 {};
class Event2 {};
class MultiListener : public EventSource<Event1>::Listener, public EventSource<Event2>::Listener
{
public:
void handle( const Event1& e );
void handle( const Event2& e );
};
class MultiSource : public EventSource<Event1>, public EventSource<Event2>
{
public:
void addMultiListener( MultiListener* ptr )
{
addListener<Event1>( ptr );
addListener<Event2>( ptr ); // this line causes error
}
};
Why does this not fall under SFINAE?
I know I can explicitly specify base class, but templates look much better than EventSource<Event1>::addListener(...)
.
Both lines cause error, as name look-up is ambiguous (before checking if overloads are viable).
You might fix issue by using using
class MultiSource : public EventSource<Event1>, public EventSource<Event2>
{
using EventSource<Event1>::addListener;
using EventSource<Event2>::addListener;
public:
void addMultiListener( MultiListener* ptr )
{
addListener<Event1>( ptr );
addListener<Event2>( ptr );
}
};