简体   繁体   中英

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 ?

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.

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