Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can non-type template parameters in c++17 be decltype(auto)?

I discovered that gcc and clang allow to use decltype(auto) in non-type template parameter type clause. E.g.:

template <decltype(auto)>
struct X {};

int foo ;

int main() {
    X<(foo)> x;
    static_cast<void>(x);
}

[live demo gcc] [live demo clang]

Is it standard compliant feature or is it some gnu extension?

like image 535
W.F. Avatar asked Sep 03 '17 19:09

W.F.


1 Answers

This is standard. First, for a non-type template parameter:

[temp.param/4]

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

  • ...
  • a type that contains a placeholder type.

Where placeholder types have the following specified:

[dcl.spec.auto/1]

The auto and decltype(auto) type-specifiers are used to designate a placeholder type that will be replaced later by deduction from an initializer. The auto type-specifier is also used to introduce a function type having a trailing-return-type or to signify that a lambda is a generic lambda ([expr.prim.lambda.closure]). The auto type-specifier is also used to introduce a structured binding declaration.

[dcl.spec.auto/5]

A placeholder type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression and as a decl-specifier of the parameter-declaration's decl-specifier-seq in a template-parameter.

Since the bullet above says "placeholder type", and such a type can be designated either with auto or decltype(auto), both compilers are correct.

like image 163
StoryTeller - Unslander Monica Avatar answered Nov 07 '22 14:11

StoryTeller - Unslander Monica