简体   繁体   English

为什么这些超载不明确?

[英]Why aren't these overloads ambiguous?

The following code compiles fine with gcc and clang. 以下代码使用gcc和clang编译好。

template <typename T>
struct identity
{
    typedef T type;
};

template <typename T>
void foo(typename identity<T>::type);

template <typename T>
void foo(T);

int main()
{
    foo<int>(0);
}

It looks like overload resolution is choosing the first overload (the identity<T>::type one). 看起来重载决策是选择第一个重载( identity<T>::type 1)。

Could someone explain why the overloads aren't ambiguous? 有人可以解释为什么重载不是模棱两可的吗? As far as I can tell, the only difference between them is that the argument of the first one is a non-deduced context and the argument of the second one isn't, but since I'm providing the template argument explicitly, I don't see why that should matter. 据我所知,它们之间的唯一区别是第一个的参数是非推导的上下文而第二个的参数不是,但是因为我明确地提供了模板参数,所以我不要不明白为什么这很重要。

Both overloads are viable, but the former is more specialized than the latter, and therefore it gets picked by overload resolution. 两种重载都是可行的,但前者比后者更专业 ,因此它可以通过重载决策来选择。

Per paragraph 13.3.3/1 of the C++11 Standard on overload resolution: 根据关于重载决策的C ++ 11标准的第13.3.3 / 1段:

[...] a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i , ICSi(F1) is not a worse conversion sequence than ICSi(F2) , and then [...]一个可行的函数F1被定义为比另一个可行函数F2更好的函数如果对于所有参数iICSi(F1)不是比ICSi(F2)更差的转换序列,然后

— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2) , or, if not that, - 对于某些参数j, ICSj(F1)是比ICSj(F2)更好的转换序列,或者,如果不是,

— the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (ie, the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type. - 上下文是通过用户定义的转换(参见8.5,13.3.1.5和13.3.1.6)初始化以及从返回类型F1到目标类型的标准转换序列(即,正在初始化的实体的类型)是一个比从F2的返回类型到目标类型的标准转换序列更好的转换序列。 [ ... ] or, if not that, [...]或者,如果不是,

F1 is a non-template function and F2 is a function template specialization, or, if not that, - F1是非模板函数, F2是函数模板特化,或者,如果不是,

F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2. - F1F2是功能模板特化,根据14.5.6.2中描述的偏序规则, F1的功能模板比F2的模板更专业。

The process of determining which of two function templates is more specialized than the other is outlined in paragraph 14.5.6.2/2: 第14.5.6.2/2段概述了确定两个功能模板中哪一个比另一个更专业的过程:

Partial ordering selects which of two function templates is more specialized than the other by transforming each template in turn (see next paragraph) and performing template argument deduction using the function type. 部分排序通过依次转换每个模板(参见下一段)并使用函数类型执行模板参数推导来选择两个函数模板中哪一个比另一个更专业。 The deduction process determines whether one of the templates is more specialized than the other. 演绎过程确定其中一个模板是否比另一个模板更专业。 If so, the more specialized template is the one chosen by the partial ordering process. 如果是这样,则更专业的模板是由部分排序过程选择的模板。

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

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