mardi 24 mai 2016

Could not deduce template argument for overloaded function

I've got a simple function called function for example with the following signature:

class Type { /* ... */ };

bool function( const Type& aValue ) { /* ... */ return true; }

I've got some other classes and I wanted to overload the mentioned function so that only the class derived from Base could use it:

class Base { /* ... */ };
class Derived : public Base { /* ... */ };

template < typename T >
bool function( const typename std::enable_if< std::is_base_of< Base, T >::value, T >::type& aValue ) { /* ... */ return true; }

It is working fine if I use it like this:

Derived object;
function< Derived >( object );

but if I leave the template argument I get the mentioned error (could not deduce template argument):

Derived object;
function( object ); // Compilation error (C2664).

Is there any solution where I can leave the template argument ?

(MSVC 2012)

Aucun commentaire:

Enregistrer un commentaire