Home > Backend Development > C++ > body text

Why Are Redundant Template Parameter Lists in C 20 Class Template Constructors Now Forbidden?

Barbara Streisand
Release: 2024-11-28 02:10:10
Original
304 people have browsed it

Why Are Redundant Template Parameter Lists in C  20 Class Template Constructors Now Forbidden?

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>();
};
Copy after login

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.
Copy after login

Specifically, the id-expression in the constructor declarator must now take one of the following forms:

  • In a member declaration within a non-friend member specification of a class, the injected-class-name of the enclosing class.
  • In a member declaration within a non-friend member specification of a class template, the class-name naming the current instantiation of the enclosing class template.

Therefore, the correct constructor declaration in C 20 is simply:

template<typename T>
struct S {
    S();
};
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template