简体   繁体   中英

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)

By introducing a nested name specifier ( <T>:: ) you inhibit template type deduction on T .

That said, you have to let the argument expression type to be deduced by putting the enable_if elsewhere, eg, in a return type syntax:

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

or in a template type parameter list:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM