简体   繁体   English

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

[英]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. 由于上面的项目符号表示“占位符类型”,并且可以使用autodecltype(auto)来指定这种类型,因此两种编译器都是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++ 17可以推导出具有显式非类型参数的`auto`非类型`模板`参数模式匹配模板吗? - Can C++17's deduced `auto` non-type `template` parameters pattern-match templates with explicit non-type parameters? C++17和C++11中的非类型模板参数有什么区别? - What is the difference between non-type template parameters in C++17 and C++11? Clang和GCC在铸造C ++ 17中对非类型模板参数的auto说明符不同意 - Clang and GCC disagree in auto specifier for non-type template parameter in a casting C++17 C ++ 17,为什么自动非类型模板参数不能与SFINAE一起使用 - C++17, why doesn't auto non-type template parameter work with SFINAE 在 C++ 非类型模板参数中使用 decltype(auto) - Using decltype(auto) in C++ non-type template parameter 在模板模板参数的情况下,decltype(auto)可转发的参考非类型模板参数是否可转发 - Are reference non-type template parameters deduced by decltype(auto) forwardable in case of template template parameter 我可以使用C ++ 17 captureless lambda constexpr转换运算符的结果作为函数指针模板非类型参数吗? - Can I use the result of a C++17 captureless lambda constexpr conversion operator as a function pointer template non-type argument? 模板模板类中非类型参数的类型在 C++14 中不可推导,但在 C++17 中可推导 - Type of non-type parameter in template template class is non deducible in C++14 but deducible in C++17 C++17中模板参数中auto的优点 - Advantages of auto in template parameters in C++17 在C ++ 14中使用auto实现非类型模板参数 - Achieving non-type template parameters with auto in C++14
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM