Removal of Redundant Template Parameter Lists in Class Template Constructors in C 20
In C 17, it was acceptable for class template constructors to have redundant template parameter lists. For example:
template<typename T> struct S { S<T>(); };
However, in C 20, this is no longer the case. A recent change mandates that all constructors for class templates must use the injected class name, which eliminates redundancy in the declarator.
This alteration is documented in the compatibility section of the C 20 draft:
[diff.cpp17.class] **Affected subclauses**: [class.ctor] and [class.dtor] **Change**: A simple-template-id is no longer valid as the declarator-id of a constructor or destructor. **Rationale**: Remove potentially error-prone option for redundancy. **Effect on original feature**: Valid C++ 2017 code may fail to compile in this International Standard.
Specifically, the id-expression in the constructor declarator must now take one of the following forms:
Therefore, the correct constructor declaration in C 20 is simply:
template<typename T> struct S { S(); };
The above is the detailed content of Why Are Redundant Template Parameter Lists in C 20 Class Template Constructors Now Forbidden?. For more information, please follow other related articles on the PHP Chinese website!