[英]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. 我发现gcc和clang允许在非类型模板参数type子句中使用
decltype(auto)
。 Eg: 例如:
template <decltype(auto)>
struct X {};
int foo ;
int main() {
X<(foo)> x;
static_cast<void>(x);
}
[live demo gcc] [live demo clang] [现场演示gcc] [现场演示clang]
Is it standard compliant feature or is it some gnu extension? 它是符合标准的功能还是某些gnu扩展?
This is standard. 这是标准的。 First, for a non-type template parameter:
首先,对于非类型模板参数:
[temp.param/4] [temp.param / 4]
A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
非类型模板参数应具有以下(可选,具有cv限定)类型之一:
- ...
...
- a type that contains a placeholder type .
包含占位符类型的类型 。
Where placeholder types have the following specified: 其中占位符类型指定了以下内容:
[dcl.spec.auto/1] [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.
auto和decltype(auto)类型说明符用于指定占位符类型 , 该占位符类型稍后将通过初始化程序的推导来替换。 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]).
自动类型说明符还用于引入具有尾随返回类型的函数类型,或表示lambda是通用lambda([expr.prim.lambda.closure])。 The auto type-specifier is also used to introduce a structured binding declaration.
自动类型说明符还用于引入结构化绑定声明。
[dcl.spec.auto/5] [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.
占位符类型也可以在new-expression的new-type-id或type-id中的type-specifier-seq中使用 ,并在template-template中用作参数声明的decl-specifier-seq的decl-specifier。参数。
Since the bullet above says "placeholder type", and such a type can be designated either with auto
or decltype(auto)
, both compilers are correct. 由于上面的项目符号表示“占位符类型”,并且可以使用
auto
或decltype(auto)
来指定这种类型,因此两种编译器都是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.