Search code examples
c++standardsstandards-compliance

Inconsistent behaviour accessing private nested class definition


I have code that declares a subclass template as private and then a member as protected:

class X {
private:
    template <class T>
    class Y {
    public:
        void somethingToDo();
        // definition
    };
protected:
    Y<SomeType> _protectedMember;
    // More definition
};

class Z : public virtual X{
public:
    void f();
}
void Z::f() {
    ...
    _protectedMember.somethingToDo();
}

Originally I compiled this with gcc 4.3.4 and it accepted it. I then sent it off to try and build against GCC, IBM and Microsoft compilers on various platforms and the non-gcc compilers rejected it. Now that seems to be an indictment of (this version of) gcc's standards compliance. But before I come to any conclusions, I'd like to verify what is technically correct.

Thanks.


Solution

  • Your program appears valid to me (well, except for Y::somethingToDo being nonsensically private). Z::f() isn't asking for access to any private names, only protected ones.

    If Z::f() tried to reference Y<T>, then the compiler should err. But Z::f() is only accessing _protectedMember, which is surely allowed.