Vincent
Vincent

Reputation: 60371

Function overloading and template deduction 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 with no relation with MyClass, the first version will be called. If I call f with a MyClass type (whatever the template parameters type are) then the second version will be called. But now, consider :

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

What version of the function will be called for a MyDerivedClass type ?

Upvotes: 4

Views: 859

Answers (1)

Andreas Brinck
Andreas Brinck

Reputation: 52519

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

Each of these contexts defines the set of candidate functions and the list of arguments in its own unique way. But, once the candidate functions and argument lists have been identified, the selection of the best function is the same in all cases: — First, a subset of the candidate functions—those that have the proper number of arguments and meet certain other conditions—is selected to form a set of viable functions (13.3.2). — Then the best viable function is selected based on the implicit conversion sequences (13.3.3.1) needed to match each argument to the corresponding parameter of each viable function.

The first one is a better match since it won't involve any implicit conversion.

Upvotes: 6

Related Questions