为什么我可以通过模板类扩展私有嵌套类?

Mat*_*_E_ 5 c++ inheritance templates nested

我遇到了一些奇怪的事情,似乎模板类可以扩展私有嵌套类.

给定以下私有嵌套类:

class A {
private:
  class B {
  protected:
    void doSomething() {
      ...
    }
  };
};
Run Code Online (Sandbox Code Playgroud)

以下内容未按预期编译:

class C : public A::B {
public:
  C() {
    this->doSomething();
  }
};
Run Code Online (Sandbox Code Playgroud)

然而,gcc似乎很乐意接受以下编译,没有呜咽,实际上调用方法:

template<typename T>
class C : public A::B {
public:
  C() {
    this->doSomething();
  }
};
Run Code Online (Sandbox Code Playgroud)

有没有人知道这是否是使用模板时的预期行为,或者我在gcc中发现了一个奇怪的现象.我在版本4.4.5(Ubuntu/Linaro 4.4.4-14ubuntu5)所以我意识到我有点过时了.如果这是预期的行为,我会非常感谢解释(或指向解释的指针),因为它不是我所期待的,我想知道更多关于它的信息.

非常感谢,马特

Dav*_*eas 7

这应该是编译器错误.无法从任何不是朋友的类访问该类A,包括类模板的任何实例化.

GCC 4.2.1和4.6接受该代码

Clang ++使用错误消息拒绝它

error: 'B' is a private member of 'A'
  struct C : A::B {
Run Code Online (Sandbox Code Playgroud)

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]"
Run Code Online (Sandbox Code Playgroud)