同样的问题:为什么GCC允许从私有嵌套类继承? 对于非模板类,它允许从私有嵌套类继承, 如果是朋友,但不是模板类。 这是一个错误吗?
template<class Base>
class InheritFromBaseMember : public Base::MemberPrivate // error
{
using PrivateMember = typename Base::MemberPrivate; // works fine
};
class MyBase{
friend class InheritFromBaseMember<MyBase>;
// another try to declare it friend
template<class T>
friend class InheritFromBaseMember;
friend class AnotherClass;
class MemberPrivate{};
};
class AnotherClass : public MyBase::MemberPrivate{}; // works fine
int main() {
InheritFromBaseMember<MyBase>{};
}
g ++ 5.3.0的错误消息:
error: 'class MyBase::MemberPrivate' is private
class MemberPrivate{};
^
error: within this context
class InheritFromBaseMember : public Base::MemberPrivate // error
^
答案 0 :(得分:6)
这绝对是一个gcc bug。 gcc拥有lots of issues友情和模板。这个例子几乎完全出现在标准的[class.friend]下,强调我的:
声明一个类成为朋友意味着该类中的私有成员和受保护成员的名称 授予友谊可以在 base-specifiers 和友好类的成员声明中访问。
[例如:class A { class B { }; friend class X; }; struct X : A::B { // OK: A::B accessible to friend A::B mx; // OK: A::B accessible to member of friend class Y { A::B my; // OK: A::B accessible to nested member of friend }; };
-end example]