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

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

随着将 class 类型的非类型模板参数添加到 C++20 标准 (P0732R2) 中,引入了使用声明非类型模板参数的可能性 非类型模板参数 [dcl.type.class.deduct]§2:

的推断 class 类型的占位符

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.

加粗的是在标准中增加了P0732R2。这允许这样的代码:

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

template <x v>
struct y {};

y <1> b;

compiles with GCC.

的代码

我考虑过在新代码中使用这个特性,但有 3 个原因让我认为这个特性实际上应该从标准中删除:

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

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

是的。 [temp.param]/6 对此很清楚:

A non-type template-parameter shall have one of the following (possibly cv-qualified) types:

  • a structural type (see below),
  • a type that contains a placeholder type ([dcl.spec.auto]), or
  • a placeholder for a deduced class type ([dcl.type.class.deduct]).

至于你的顾虑。

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

是的,语言添加经常不完整并导致语言问题。一个比较紧迫的是限制什么类型可以作为non-type模板参数,这个限制禁止std::stringstd::tuplestd::optional和moment。这些都会及时解决。

This syntax for placeholder inside a parameter looks like the "short concept syntax" of the concept-TS

这就是 CTAD 语法的样子。

On std-discussion, it looked like not all committee members are aware of this feature.

那么?