为什么Visual C++无法编译从私有嵌套类继承的朋友模板?

vit*_*aut 6 c++ gcc compiler-errors g++ visual-c++

请考虑以下代码:

class A {
  class B {};

  template <typename T>
  friend class C;
};

template <typename T>
class C : A::B {};

int main() { C<int> c; }
Run Code Online (Sandbox Code Playgroud)

它与GCC和Clang编译良好,但Visual C++ 2010给出错误:

test.cc(11) : error C2248: 'A::B' : cannot access private class declared in class 'A'
Run Code Online (Sandbox Code Playgroud)

它是Visual C++中的一个错误还是我错过了什么?

chi*_*ill 2

1998 年标准和 2011 年标准都包含基本相同的代码作为示例,分别在 14.5.3 #4 和 14.5.4 #3 中。

class X {
  template<class T> friend struct A;
  class Y { };
};
template<class T> struct A { X::Y ab; }; // OK
template<class T> struct A<T*> { X::Y ab; }; // OK
Run Code Online (Sandbox Code Playgroud)

显然这是一个错误。

  • @MichaelBurr,很有趣,VS2010/VS2012/VS Nov 2012 CTP 在 OP 代码上给出错误,但在 `template &lt;typename T&gt; class C : A::B { A::B x; 上成功。};`:D (3认同)