我发现 gcc 和 clang 允许在非类型模板参数类型子句中使用 decltype(auto)
。例如:
template <decltype(auto)>
struct X {};
int foo ;
int main() {
X<(foo)> x;
static_cast<void>(x);
}
[live demo gcc] [live demo clang]
它是符合标准的功能还是某种 gnu 扩展?
最佳答案
这是标准的。首先,对于非类型模板参数:
A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
- ...
- a type that contains a placeholder type.
其中占位符类型指定了以下内容:
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.
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.
由于上面的项目符号说的是“占位符类型”,并且这种类型可以用 auto
或 decltype(auto)
指定,所以两种编译器都是正确的。
关于c++ - c++17 中的非类型模板参数可以是 decltype(auto) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907649/