decltype(auto) as non-type template-parameter



template <auto N>
struct S { /*...*/ };
// または
template <decltype(auto) N>
struct S { /*...*/ };

S<42> obj;

C++17, p5, 17.1/p4より一部引用(下線部は強調)。

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 ( The auto type-specifier is also used to introduce a structured binding declaration (11.5).

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 (8.3.4) and as a decl-specifier of the parameter-declaration's decl-specifier-seq in a template-parameter (17.1).

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

  • (snip)
  • a type that contains a placeholder type (