Can an identity alias template be a forwarding reference?

后端 未结 2 1365
失恋的感觉
失恋的感觉 2021-01-04 02:41

Consider the following snippet below:

template 
using identity = T;

template 
void foo(identity&&) {}

int ma         


        
2条回答
  •  花落未央
    2021-01-04 03:27

    It is not a forwarding reference. C++14 (n4140) 14.8.2.1/3 (emphasis mine):

    ... If P is an rvalue reference to a cv-unqualified template parameter and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.

    This is the piece of the standard which specifies how forwarding references work. P, the type of the function parameter, is of type "rvalue reference to identity." identity is the type of the template parameter, but it is not a template parameter itself, so the forwarding reference deduction rule does not apply.

    We can also look at what 14.5.7/2 has to say about alias templates:

    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.

    So the substituted alias is equivalent to the type of T, but 14.8.2.1/3 reads "reference to ... template parameter," not "reference to ... the type of a template parameter."

提交回复
热议问题