Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Visual C++ fail to compile a friend template inheriting from a private nested class?

Consider the following code:

class A {
  class B {};

  template <typename T>
  friend class C;
};

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

int main() { C<int> c; }

It compiles fine with GCC and Clang but Visual C++ 2010 gives an error:

test.cc(11) : error C2248: 'A::B' : cannot access private class declared in class 'A'

Is it a bug in Visual C++ or am I missing something?

like image 376
vitaut Avatar asked Dec 12 '12 16:12

vitaut


2 Answers

Both the 1998 standard and the 2011 standard contain essentially the same code as an example, in 14.5.3 #4 and 14.5.4 #3, respectively.

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

Apparently it's a bug.

like image 50
chill Avatar answered Oct 17 '22 06:10

chill


Yes. That is most certainly a bug in the Visual C++ compiler.

Footnote: the GCC compiler compiles it fine.

like image 28
Nawaz Avatar answered Oct 17 '22 06:10

Nawaz