模板:名称解析 --> 该声明在继承时是否正确?
这是 ISO C++ 标准 14.6/6 中的声明:
在类模板的定义内或类模板成员的定义内,当引用先前声明的类模板成员的非限定名称时,不需要关键字
typename
声明一个类型。当使用限定名引用成员时,即使限定符只是类模板名称,也应始终指定关键字typename
。 [示例:模板
结构体A { typedef int B; A::B b; // 格式错误:A::B 之前需要类型名 无效 f(A ::B); // 格式错误:A ::B 之前需要类型名 类型名 A::B g(); // 好的 }; 无论限定名称是
A
还是A
,都需要关键字 typename,因为A
或A
是类模板中与参数列表的同义词。 ]
继承时这个说法是否正确?
如果是,有人可以解释一下吗?
我检查了内部类;它被接受了吗?但我无法检查继承?
This is the statement from ISO C++ Standard 14.6/6:
Within the definition of a class template or within the definition of a member of a class template, the keyword
typename
is not required when referring to the unqualified name of a previously declared member of the class template that declares a type. The keywordtypename
shall always be specified when the member is referred to using a qualified name, even if the qualifier is simply the class template name. [Example:template<class T> struct A { typedef int B; A::B b; // ill-formed: typename required before A::B void f(A<T>::B); // ill-formed: typename required before A<T>::B typename A::B g(); // OK };
The keyword typename is required whether the qualified name is
A
orA<T>
becauseA
orA<T>
are synonyms within a class template with the parameter list<T>
. ]
Is this statement is true while inheritance?
If yes, can anyone explain this?
I checked with inner class; it is accepted? But I am unable to check with inheritance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,继承成员也是如此。
基模板的成员需要关键字
typename
,但一般来说基类不需要。基本模板需要它的原因是它们的成员不会自动带入class {}
块的范围,因此引用它们的唯一方法是使用限定 ID,这需要类型名称
。Yes, that is equally true of inherited members.
The keyword
typename
is required for members of base templates, but not base classes in general. The reason it is required for base templates is that their members are not automatically brought into the scope of theclass {}
block, so the only way to refer to them is with a qualified-id, which requirestypename
.