简体   繁体   English

非类型模板参数的推导 class 类型的占位符是 C++20 功能吗?

[英]Is placeholder for the deduced class type of non-type template parameter a C++20 feature?

With the addition of non-type template parameter of class type into the C++20 standard ( P0732R2 ) was introduced the possibility to declare a non-type template parameter using a placeholder for deduced class type for non-type template parameter [dcl.type.class.deduct]§2 :随着将 class 类型的非类型模板参数添加到 C++20 标准 ( P0732R2 ) 中,引入了使用非类型模板参数推导的 class 类型的占位符声明非类型模板参数的可能性[dcl. type.class.deduct]§2

A placeholder for a deduced class type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression, as the simple-type-specifier in an explicit type conversion (functional notation), or as the type-specifier in the parameter-declaration of a template-parameter.推导的 class 类型的占位符也可用于新表达式的新类型标识或类型标识中的类型说明符序列中,作为显式类型转换中的简单类型说明符(功能符号),或作为模板参数的参数声明中的类型说明符。

What is bolded is the addition of P0732R2 to the standard.加粗的是标准中增加了P0732R2 This allows such code:这允许这样的代码:

template <class T>
struct x{
   constexpr x(T){}
};

template <x v>
struct y {};

y <1> b;

Code that compiles with GCC.使用 GCC 编译的代码。

I thought about using this feature in new code, but there is 3 reasons that makes me think that actually this feature was intended to be removed from the standard:我曾考虑在新代码中使用此功能,但有 3 个原因让我认为该功能实际上是打算从标准中删除的:

  • It is not well integrated in the standard, for example, the unchanged standard wording for partial ordering of function templates makes it impossible to partially specialize a class template similar to y because the synthesized argument for the template parameter v must have a unique invented type (the synthesized argument used during partial ordering of class template specializations);它没有很好地集成在标准中,例如,对于 function 模板的部分排序的未更改标准措辞使得不可能部分特化类似于y的 class 模板,因为模板参数v的合成参数必须具有唯一的发明类型(在 class 模板特化的部分排序期间使用的综合参数);

  • This syntax for placeholder inside a parameter looks like the "short concept syntax" of the concept-TS.参数内占位符的这种语法看起来像概念 TS 的“短概念语法”。 This syntax has been changed to the "adjective syntax" concept-name auto after the addition of P0732R2 to the standard;在标准中加入 P0732R2 后,此语法已更改为“形容词语法” concept-name auto

  • On std-discussion, it looked like not all committee members are aware of this feature.在标准讨论中,似乎并非所有委员会成员都知道此功能。

Is placeholder for the deduced class type of non-type template parameter a C++20 feature?非类型模板参数的推导 class 类型的占位符是 C++20 功能吗?

Is placeholder for the deduced class type of non-type template parameter a C++20 feature?非类型模板参数的推导 class 类型的占位符是 C++20 功能吗?

Yes.是的。 [temp.param]/6 is quite clear about this: [temp.param]/6对此非常清楚:

A non-type template-parameter shall have one of the following (possibly cv-qualified) types:非类型模板参数应具有以下类型之一(可能是 cv 限定的):

  • a structural type (see below),结构类型(见下文),
  • a type that contains a placeholder type ([dcl.spec.auto]), or包含占位符类型 ([dcl.spec.auto]) 的类型,或
  • a placeholder for a deduced class type ([dcl.type.class.deduct]).推导的 class 类型的占位符([dcl.type.class.deduct])。

As for your concerns.至于你的顾虑。

It is not well integrated in the standard, for example, the unchanged standard wording for partial ordering of function templates makes it impossible to partially specialize a class template [...]它没有很好地集成到标准中,例如,对于 function 模板的部分排序的标准措辞没有改变,因此无法对 class 模板进行部分特化 [...]

Yes, language additions are frequently incomplete and lead to language issues.是的,语言添加经常不完整并导致语言问题。 A more pressing one is the limitation on what kinds of types can be used as non-type template parameters, a limitation that prohibits std::string , std::tuple , and std::optional and the moment.一个更紧迫的问题是对哪些类型可以用作非类型模板参数的限制,一个禁止std::stringstd::tuplestd::optional的限制和时刻。 These will be resolved in due time.这些将在适当的时候得到解决。

This syntax for placeholder inside a parameter looks like the "short concept syntax" of the concept-TS参数内占位符的这种语法看起来像 concept-TS 的“短概念语法”

This is just what CTAD syntax looks like.这正是 CTAD 语法的样子。

On std-discussion, it looked like not all committee members are aware of this feature.在标准讨论中,似乎并非所有委员会成员都知道此功能。

So?所以?

暂无
暂无

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

相关问题 在 C++20 中是否允许通过非类型模板参数中的类类型传递函数指针? - Is passing of a function pointer through a class type in non-type template parameter allowed in C++20? 使用非类型模板参数的 C++20 概念对 class 模板进行完全特化 - Full specialisation of a class template using a C++20 concept of a non-type template parameter c++ 20中如何静态断言该类型对于模板非类型参数是可行的 - How to static_assert that type is viable for template non-type parameter in c++20 C++20:非类型模板参数中的非捕获 lambda - C++20: Non-capturing lambda in non-type template parameter 可以使用占位符为推导的类类型指定类型的非类型模板参数传递给C ++ 2a中的另一个模板吗? - Can a non-type template argument whose type is specified with a placeholder for a deduced class type be passed to another template in C++2a? 结构非类型模板参数+聚合初始化:c++20标准与否 - Struct non-type template parameter + agregate initialization: standard in c++20 or not C++20 非类型模板参数,即先前类型参数中的模板:不是有效的模板参数,因为不是变量 - C++20 non-type template parameter which is template in the prior type parameters: is not a valid template arg, because is not a variable 推导(非类型)模板参数类型的编译器差异 - Compiler variance for the type of deduced (non-type) template parameter C++20 标准对使用子对象作为模板非类型 arguments 有什么看法? - What does the C++20 standard say about usage of subojects as template non-type arguments? C ++:朋友模板类/模板非类型参数 - C++: friend template class / template non-type parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM