Given:
template <typename T>
class C {
C & operator ++ () { ... }
};
Why/how is C
allowed to declare variables and functions of type C
rather than being required to name C<T>
? I had not really thought about it before working on a template with many parameters that would make spelling out the "self type" inconvenient.
Are there any quirks of this I should know about?
[n3290: 14.6.1/1]:
Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injected-class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in<>
.
Ostensibly, it's merely a convenience feature.
It's just syntactic sugar.
It is convenient to not have to change the signatures of your methods if you have to change the template parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With