C ++的什么功能让模板类引用自己没有模板参数? [英] What feature of C++ lets template classes refer to themselves without template arguments?
问题描述
给定:
template <typename T>
class C {
C & operator ++ () { ... }
};
为什么/ C
变量和函数 C
,而不需要命名 C
?我在工作之前没有真正考虑过这个模板,有很多参数,这将使拼写出自我类型不方便。
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.
有什么怪异的知道吗?
推荐答案
[n3290:14.6.1 / 1] :
与正常(非模板)类类似, class
模板有一个inject-class-name(第9条)。
inject-class-name可以用作 template-name 或 type-name 。
当与 template-argument-list 一起使用时,作为模板的
模板参数 template-parameter ,或者作为朋友类
模板声明的 elaborated-type-specifier 中的最终标识符,它引用类模板本身。
否则,它等同于<$ c>中的类模板的模板名称后跟
模板参数 $ c><> 。
[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.
这篇关于C ++的什么功能让模板类引用自己没有模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!