9

Same question is asked: Why does GCC allow inheriting from a private nested class? For non template classes, its allowed to inherit from private nested classes, if it is a friend, but not for template classes. Is it a bug?

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>{};
}

Errormessage from g++ 5.3.0:

error: 'class MyBase::MemberPrivate' is private
     class MemberPrivate{};
           ^
error: within this context
 class InheritFromBaseMember : public Base::MemberPrivate // error
       ^
Barry
  • 286,269
  • 29
  • 621
  • 977
gerdi
  • 165
  • 6
  • 1
    Are you sure about the compiler version? It compiles [here](https://godbolt.org/g/1uUkge) just fine with g++ 4.9.0 and greater, including 5.3.0. But not with earlier versions. – eerorika May 27 '16 at 16:05
  • Well, I translate with Eclipse/CDT Cygwin Miscellaneous --version and the output is: g++ (GCC) 5.3.0 – gerdi May 31 '16 at 07:36

1 Answers1

6

This is definitely a gcc bug. gcc has lots of issues with friendship and templates. This example almost exactly appears in the standard, under [class.friend], emphasis mine:

Declaring a class to be a friend implies that the names of private and protected members from the class granting friendship can be accessed in the base-specifiers and member declarations of the befriended class.
[ Example:

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 ]

Barry
  • 286,269
  • 29
  • 621
  • 977