Home  >  Article  >  Backend Development  >  How do Class Names in Class Templates Work Without Template Parameters?

How do Class Names in Class Templates Work Without Template Parameters?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 11:46:31356browse

How do Class Names in Class Templates Work Without Template Parameters?

Class Names in Class Templates Without Template Parameters

Consider a class template defined as follows:

<code class="cpp">template <typename E>
class Link {
public:
    E element;
    Link* next;  // No typename argument here
};</code>

The code you provided features a public member Link* next without a typename argument. This is known as an "injected class name."

According to [temp.local], when the injected class name is used within the scope of the class, it is equivalent to the class name followed by angle brackets containing the template parameters of the class. In this case, Link effectively becomes Link.

Therefore, the expression Link* next within the Link class is equivalent to Link* next. This convenience feature allows the class to reference itself using its full name without explicitly specifying the template arguments.

The above is the detailed content of How do Class Names in Class Templates Work Without Template Parameters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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