c++ - c++17 中的非类型模板参数可以是 decltype(auto) 吗?

标签 c++ templates language-lawyer c++17

我发现 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 扩展?

最佳答案

这是标准的。首先,对于非类型模板参数:

[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.

其中占位符类型指定了以下内容:

[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.

由于上面的项目符号说的是“占位符类型”,并且这种类型可以用 autodecltype(auto) 指定,所以两种编译器都是正确的。

关于c++ - c++17 中的非类型模板参数可以是 decltype(auto) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907649/

相关文章:

c++ - 从参数包中提取成员类型

c++ - 它在 C++ 标准中的哪个位置记录了用户定义类型的 I/O?

c++ - 以符合标准的方式使用与数组相同类型的成员重新解释结构

c++ - 指向包含 vector<int> 问题的实例类的指针

c++ - 如何以线程安全的方式退出 C++03 中的程序?

c++ - 如何使用自定义 boost::log 格式化程序输出 TimeStamp 和 ThreadID 属性?

php - 模板语言与纯 PHP

c++ - 模板特化和从其他模板类继承模板类

c - C语言中为什么char是1个字节

c++ - 在单元测试中,如何在不使用运算符== 的情况下比较两个对象,这可能会错过新成员?