具有auto的模板功能的重载解析 [英] overload resolution of template function with auto

查看:74
本文介绍了具有auto的模板功能的重载解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下3个超载

 模板< class T>自动foo(){返回1;}模板< class T>int foo(){返回2;}模板< class T>T foo(){return 3;} 

以下疾病是不是形成了?

  static_cast< int(*)()>(& foo< int>)(); 

Clang选择重载2,而gcc无法编译(演示)

在删除#1过载时,双方都同意选择#2过载(演示)

在删除#2重载时,gcc选择#1重载并且clang无法编译(演示)

解决方案

按照[over.over]/2 ,我们执行模板参数推导.这将对所有三个重载都成功:在第一个重载中,保持 [temp.deduct.funcaddr]/2 记住:

函数模板的返回类型中的占位符类型(7.1.7.4)是非推断上下文.如果模板此类函数的参数推导成功,返回类型由实例化确定功能主体.

由于推论将成功(假设所有模板参数均已明确提供参数),因此将返回类型推导为 int .在第二种情况下,由于提供了参数,推导成功,而在第三种情况下,将推导 T .

继续进行第4段

如果选择了多个功能,则任何给定如果集合中包含功能模板专门化 F1 第二个功能模板专业化,其功能模板为根据 F1 的功能模板,14.5.6.2的部分排序规则删除后(如果有),应仅保留一个选定的功能.

根据 [temp.deduct.partial]/3 ,功能模板的功能类型用于部分排序.我们可以立即看到#1和#2的函数类型不包含任何参与推导的模板参数,因此通过添加此处解释了为什么该分辨率有问题;最重要的是,排序只会对#1和#2产生歧义.

也就是说,根据当前的(可能是有缺陷的)措辞,转换在所有情况下都是不正确的.如果对成对的非依存/推论参数,部分排序是固定的,则

  • 情况1和3是模棱两可的,因为对于两个非依赖性函数类型(#1和#2中的一种),没有排序对.
  • 第2种情况下的接受行为是正确的(符合预期).

[temp.deduct.类型]/8 元素9( T()),以防您好奇.

With the 3 following overloads

template <class T> auto foo() { return 1; }
template <class T> int  foo() { return 2; }
template <class T> T    foo() { return 3; }

Is the following ill formed ?

static_cast<int(*)()>(&foo<int>)();

Clang selects overload #2, whereas gcc fails to compile (Demo)

When removing overload #1, both agree to select overload #2 (Demo).

When removing overload #2, gcc selects overload #1 and clang fails to compile (Demo)

解决方案

As per [over.over]/2, we perform template argument deduction. This will succeed for all three overloads: in the first one, keep [temp.deduct.funcaddr]/2 in mind:

A placeholder type (7.1.7.4) in the return type of a function template is a non-deduced context. If template argument deduction succeeds for such a function, the return type is determined from instantiation of the function body.

Since the deduction will succeed (given that all template parameters have explicitly supplied arguments), the return type is deduced as int. In the second case, deduction succeeds since the argument is provided, and in the third, T will be deduced.

Going on to paragraph 4,

If more than one function is selected, [...] any given function template specialization F1 is eliminated if the set contains a second function template specialization whose function template is more specialized than the function template of F1 according to the partial ordering rules of 14.5.6.2. After such eliminations, if any, there shall remain exactly one selected function.

According to [temp.deduct.partial]/3, the function templates' function types are used for partial ordering. We can immediately see that #1 and #2's function types do not contain any template parameters that participate in deduction, hence via the addition to [temp.deduct.partial]/4 introduced by core issue 1391's resolution, their corresponding Ps are not used to determine the ordering. @bogdan explained here why that resolution is problematic; the bottom line is that ordering just yields an ambiguity for #1 and #2.

That is, according to current (probably defective) wording, the conversion is ill-formed in all cases. If partial ordering is fixed for pairs of non-dependent/deducing parameters,

  • case 1 and 3 are ambiguous, because for two non-dependent function types (the ones of #1 and #2), there is no ordering pair.
  • the accepting behavior in case 2 is correct (as expected).

[temp.deduct.type]/8 element 9 (T()), in case you were curious.

这篇关于具有auto的模板功能的重载解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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