Home >Backend Development >C++ >Does `Link* next` Use the Injected Class Name in C Templates?
Using Class Name in Class Template Without Template Parameters
Consider the following code from a C book:
<code class="cpp">template <typename E> class Link { ... Link* next; // this line may be confusing ... };</code>
In this code, the member next is declared without specifying the template argument E, raising concerns about its correctness. Shouldn't it be Link
Explanation
This is explained by the concept of the "injected class name." As per the C standard [temp.local], when used without a template argument, the class name within the class template is equivalent to the class name followed by the template parameters enclosed in angle brackets. Therefore, in this case, Link* is equivalent to Link
This rule is intended for convenience, ensuring that the class name within the class refers to the class itself and not an external entity with the same name. In the case of class templates, it provides a shorthand notation, especially when dealing with extended template argument lists.
The above is the detailed content of Does `Link* next` Use the Injected Class Name in C Templates?. For more information, please follow other related articles on the PHP Chinese website!