Can a forwarding reference be aliased with an alias template?

◇◆丶佛笑我妖孽 提交于 2020-03-21 20:29:39

问题


This is a continuation of my previous question:

Can an identity alias template be a forwarding reference?

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?


回答1:


This is indeed standard compliant. §14.5.7/2:

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.

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:

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.

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.

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.



来源:https://stackoverflow.com/questions/29867841/can-a-forwarding-reference-be-aliased-with-an-alias-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!