Function overloading and template deduction priority

强颜欢笑 提交于 2019-12-23 12:33:10

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/13900616/function-overloading-and-template-deduction-priority

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!