Function overload and template subtraction priority

Consider the following function declaration:

template<typename T> f(const T& x); // Version 1 template<typename T1, typename T2> f(const MyClass<T1, T2>& x); // Version 2 

If I call f with a type without relation to MyClass , the first version is called. If I call f with type MyClass (regardless of the type of template parameters), then the second version is called. But now consider:

 template<typename T1, typename T2, typename T3> MyDerivedClass : public MyClass<T1, T2> {}; 

What version of function is called for type MyDerivedClass ?

+4
source share
1 answer

This is described in section 13.3 of the standard. Paragraph 13.3 / 1 states:

Each of these contexts defines a set of candidate functions and a list of arguments in its own way. But, once the candidate functions and argument lists have been identified, the choice of the best function is the same in all cases: first, a subset of the candidate functions - those that have a sufficient number of arguments and satisfy some other conditions - are selected to form a set of viable functions (13.3. 2). - then the best viable function is selected for the implicit conversion sequences (13.3.3.1) necessary to match each argument to the corresponding parameter of each viable function.

The first is a better match, since it will not include any implicit conversion.

+5
source

All Articles