简体   繁体   English

功能重载和模板扣除优先级

[英]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. 如果我使用与MyClass无关的类型调用f ,则将调用第一个版本。 If I call f with a MyClass type (whatever the template parameters type are) then the second version will be called. 如果我用MyClass类型调用f (无论模板参数类型是什么),那么将调用第二个版本。 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 ? 将为MyDerivedClass类型调用什么版本的函数?

This is handled in section 13.3 of the standard. 这在标准的第13.3节中处理。 Paragraph 13.3/1 states: 第13.3 / 1段指出:

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). 但是,一旦确定了候选函数和参数列表,最佳函数的选择在所有情况下都是相同的: - 首先,候选函数的子集 - 具有适当数量的参数并满足某些其他条件的函数 - 被选中以形成一组可行的函数(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. - 然后根据将每个参数与每个可行函数的相应参数匹配所需的隐式转换序列(13.3.3.1)来选择最佳可行函数。

The first one is a better match since it won't involve any implicit conversion. 第一个是更好的匹配,因为它不涉及任何隐式转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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