c ++ 17中的非类型模板参数可以是decltype(auto)吗? [英] Can non-type template parameters in c++17 be decltype(auto)?

查看:132
本文介绍了c ++ 17中的非类型模板参数可以是decltype(auto)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现gcc和clang允许在非类型模板参数type子句中使用decltype(auto).例如:

I discovered that gcc and clang allow to use decltype(auto) in non-type template parameter type clause. E.g.:

template <decltype(auto)>
struct X {};

int foo ;

int main() {
    X<(foo)> x;
    static_cast<void>(x);
}

[实时演示gcc] 它是符合标准的功能还是gnu扩展?

Is it standard compliant feature or is it some gnu extension?

推荐答案

这是标准设置.首先,对于非类型模板参数:

This is standard. First, for a non-type template parameter:

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

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

Where placeholder types have the following specified:

[dcl.spec.auto/1]

auto和decltype(auto)类型说明符用于指定一个 占位符类型,稍后将通过从 初始值设定项.自动类型说明符还用于引入 具有尾随返回类型的功能类型,或表示 lambda是通用lambda([expr.prim.lambda.closure]).汽车 类型说明符也用于引入结构化绑定 声明.

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]

还可以在占位符的type-specifier-seq中使用占位符类型. new表达式的new-type-id或type-id,并作为以下内容的decl-specifier 参数声明中的decl-specifier-seq 模板参数.

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)指定这种类型,因此两个编译器都是正确的.

Since the bullet above says "placeholder type", and such a type can be designated either with auto or decltype(auto), both compilers are correct.

这篇关于c ++ 17中的非类型模板参数可以是decltype(auto)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆