非型テンプレートのプレースホルダ型にはauto
またはdecltype(auto)
いずれも利用できる。素直にauto
を使うべき。
本記事の内容はStackOverflowで見つけた質問と回答に基づく。
template <auto N> struct S { /*...*/ }; // または template <decltype(auto) N> struct S { /*...*/ }; S<42> obj;
C++17 10.1.7.4/p1, p5, 17.1/p4より一部引用(下線部は強調)。
1 The
auto
anddecltype(auto)
type-specifiers are used to designate a placeholder type that will be replaced later by deduction from an initializer. Theauto
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 (8.1.5.1). Theauto
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 (10.1.7.4).
関連URL
- cppreference: placeholder type specifiers
- cpprefjp: decltype(auto), 非型テンプレートパラメータのauto宣言