转发引用可以使用别名模板进行别名吗? [英] Can a forwarding reference be aliased with an alias template?

查看:170
本文介绍了转发引用可以使用别名模板进行别名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个问题的延续:

This is a continuation of my previous question:

身份别名模板是转发引用?

似乎以下代码在Clang 3.7.0( demo )和GCC 6.0.0( demo ):

It seems that the following code works in both Clang 3.7.0 (demo) and GCC 6.0.0 (demo):

template <class T>
using forwarding_reference = T&&;

template <class T>
void foo(forwarding_reference<T>) {}

int main()
{
  int i{};
  foo(i);
  foo(1);
}

编译器是否可以将别名模板替换为转发引用,

Are the compilers right to substitute the alias template for a forwarding reference and this could be a fancy way of writing one?

推荐答案

这确实是标准兼容的。 §14.5.7/ 2:

This is indeed standard compliant. §14.5.7/2:


template-id 引用别名模板的专业化时,
等价于通过用 模板参数替换模板参数

When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template.

现在,在模板参数扣除,只检查参数的类型(在模板参数方面) - §14.8.2.1/ 1:

Now, consider that during template argument deduction, only the type of the parameter (in terms of template parameters) is inspected - §14.8.2.1/1:


模板参数扣除是通过比较每个函数
模板参数类型(调用 P

对应的参数的类型

Template argument deduction is done by comparing each function template parameter type (call it P) with the type of the corresponding argument of the call (call it A) as described below.

根据下面的描述,调用(调用 A 第一引用,参数的类型,即 forwarding_reference 等价于 T&& 。因此, P T&&& ,并且扣除不会有任何差异。

According to the first quote, the type of the parameter, i.e. forwarding_reference<T>, is equivalent to T&&. Hence P is T&& and there can be no difference regarding deduction.

委员会在关于这个确切情况的缺陷报告中也得出了同样的结论, #1700

This same conclusion was made by the committee in a defect report concerning this exact scenario, #1700:


的函数参数是相同的,无论
是直接写还是通过别名模板,扣除必须
在两种情况下以相同的方式处理。

Because the types of the function parameters are the same, regardless of whether written directly or via an alias template, deduction must be handled the same way in both cases.

这篇关于转发引用可以使用别名模板进行别名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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