从重载的模板函数中选择什么规则? [英] What are the rules for choosing from overloaded template functions?

查看:131
本文介绍了从重载的模板函数中选择什么规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码,为什么选择 foo(T *)

Given the code below, why is the foo(T*) function selected ?

它( foo(T *))仍然编译和工作正常,但G ++ v4.4.0(以及可能其他编译器)将生成两个 foo()函数:一个用于char [4],一个用于char [7]。

If I remove it (the foo(T*)) the code still compiles and works correctly, but G++ v4.4.0 (and probably other compilers as well) will generate two foo() functions: one for char[4] and one for char[7].

#include <iostream>
using namespace std;

template< typename T >
void foo( const T& )
{
    cout << "foo(const T&)" << endl;
}

template< typename T >
void foo( T* )
{
    cout << "foo(T*)" << endl;
}

int main()
{
    foo( "bar" );
    foo( "foobar" );
    return 0;
}


推荐答案

,则忽略lvalue变换。转化分为几个类别,例如资格调整( T * - > T const * ),( int [N] - > int * c $ c> void() - > void(*)()

Formally, when comparing conversion sequences, lvalue transformations are ignored. Conversions are grouped into several categories, like qualification adjustment (T* -> T const*), lvalue transformation (int[N] -> int*, void() -> void(*)()), and others.

两个候选者之间的唯一区别是一个左值转换。字符串文字是转换为指针的数组。第一个候选通过引用接受数组,因此不需要一个左值转换。第二个候选需要一个左值变换。

The only difference between your two candidates is an lvalue transformation. String literals are arrays that convert to pointers. The first candidate accepts the array by reference, and thus won't need an lvalue transformation. The second candidate requires an lvalue transformation.

因此,如果有两个候选者,通过仅查看转换,功能模板专门化同样可行,那么规则是:部分排序的两个。

So, if there are two candidates that both function template specializations are equally viable by looking only at the conversions, then the rule is that the more specialized one is chosen by doing partial ordering of the two.

让我们通过查看他们对函数参数列表的签名进行比较。

Let's compare the two by looking at their signature of their function parameter list

void(T const&);
void(T*);

如果我们选择一些独特的类型 Q 第一个参数列表并尝试匹配第二个参数列表,我们匹配 Q T * 。这将失败,因为 Q 不是指针。因此,第二个至少与第一个特殊。

If we choose some unique type Q for the first parameter list and try to match against the second parameter list, we are matching Q against T*. This will fail, since Q is not a pointer. Thus, the second is at least as specialized as the first.

如果我们反过来,我们将 Q * T const& 。引用被删除,并且toplevel限定符被忽略,剩余的 T 变为 Q * 。这是用于部分排序的目的的精确匹配,并且因此针对第一候选的第二的变换的参数列表的推导成功。由于另一方向(相对于第二方向)没有成功,所以第二候选者比第一候选者专门化 - 并且因此,如果否则将存在歧义,则重载分辨率将优选第二候选者。

If we do the other way around, we match Q* against T const&. The reference is dropped and toplevel qualifiers are ignored, and the remaining T becomes Q*. This is an exact match for the purpose of partial ordering, and thus deduction of the transformed parameter list of the second against the first candidate succeeds. Since the other direction (against the second) didn't succeed, the second candidate is more specialized than the first - and in consequence, overload resolution will prefer the second, if there would otherwise be an ambiguity.

13.3.3.2/3


标准转换序列S1是比标准转换序列S2更好的转换序列,如果[...]

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if [...]


  • S1是S2的适当子序列(比较由13.3.3.1.1定义的规范形式
    中的转换序列,排除任何Lvalue转换;同一性转换序列被认为是任何非同一性转换序列的子序列) [...]
  • S1 is a proper subsequence of S2 (comparing the conversion sequences in the canonical form defined by 13.3.3.1.1, excluding any Lvalue Transformation; the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence) or, if not that [...]

然后 3/1



  • 让ICSi(F)表示隐式转换序列将列表中的第i个参数转换为可行函数F的第i个参数的类型。13.3.3.1定义隐式转换序列,13.3.3.2定义了一个隐式转换序列是更好的转换序列

给定这些定义,可行函数F1被定义为比另一个可行函数F2更好的函数[...]

Given these definitions, 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 [...]


  • 如果对于所有参数i,ICSi(F1)不是比ICSi > F1和F2是函数模板特化,F1的函数模板根据14.5.5.2中描述的部分排序规则比F2的模板更专门,或者如果不是,[...]

最后,这里是隐式转换表,可以参与标准转换序列 13.3.3.1.1 / 3

Finally, here is the table of implicit conversions that may participate in an standard conversion sequence at 13.3.3.1.1/3.

这篇关于从重载的模板函数中选择什么规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆