Why can I extend a private nested class with a template class?

I came across some weirdness when it seems like a templated class can extend a private nested class.

Given the following private nested class:

class A {
private:
  class B {
  protected:
    void doSomething() {
      ...
    }
  };
};

The following does not compile as expected:

class C : public A::B {
public:
  C() {
    this->doSomething();
  }
};

However, gcc, fortunately, seems to accept the following, which compiles without sobbing and actually calls the method:

template<typename T>
class C : public A::B {
public:
  C() {
    this->doSomething();
  }
};

Does anyone know if this is the expected behavior when using templates, or if I found weirdness in gcc. I am in version 4.4.5 (Ubuntu / Linaro 4.4.4-14ubuntu5), so I understand that I'm a little outdated. If this is the expected behavior, I would really appreciate an explanation (or a pointer to an explanation) as this is not what I expected, and I would like to know more about it.

Thanks a lot Matt

+5
1

. , A, .

GCC 4.2.1 4.6

Clang++

error: 'B' is a private member of 'A'
  struct C : A::B {

Comeau

error: class "A::B" (declared at line 5) is inaccessible
struct C : A::B {
              ^
      detected during instantiation of class "C<T> [with T=int]"
+7

All Articles