Private template inheritance is not available in vC ++ 10

The following code compiles using GCC 4.4.6 and Comeau 4.3.10.

#include <iostream> struct A { int name; }; template<typename T> struct C : T { using T::name; }; struct B : private A { friend struct C<B>; }; int main() { C<B> o; o.name = 0; } 

It gives the following error in VC ++ 10:

 main.cpp(4): error C2877: 'A::name' is not accessible from 'A' main.cpp(10): error C2247: 'A::name' not accessible because 'B' uses 'private' to inherit from 'A' 

What a good cross compiler way that allows o.name = 0; ?

Note. Adding using A::name to B fixes the problem, but publishes the member A::name to everyone, whereas it should be visible only to a specific instance of the template, namely C<B> .

+7
source share
2 answers

Working around what @kerrekSB suggested, add using A::name; to class B :

 struct A { int name; }; template<typename T> struct C : T { using T::name; }; struct B : private A { using A::name; friend struct C<B>; }; 

your original example did not work, so class A is private to B and class C<B> is a friend of B , but when accessing the name element from the C<B> object, the line is using T::name; > creates a problem because class B does not have a name member in it. this is the search area that finds the name member when trying to access it through an object of class B

Edit:

Adding the use of A :: name to B fixes the problem, but publishes A :: name to everyone, while it should be visible only to a specific instance of the template, namely C

if in this case, just declare a statement using A::name; in a private section in class B ie

 struct B : private A { protected: using A::name; public: friend struct C<B>; }; 
+5
source

There seems to be a fundamental difference in visibility considerations between gcc and VC ++ when using declaration-member-declarations; check out this simplified example without templates:

 struct A { int name; }; struct B: private A { friend struct C; }; struct C: B {using B::name; }; int main() { C o; o.name = 0; } 

It will compile on gcc, but not on VC ++ (with the same error as in the question). You will have to consult the standard about who does it right ...

+2
source

All Articles