A derived class can access a private member of its base class

I came across a strange situation where my derived class can access a private member of its base class in which the templates are involved.


Consider the following example:

class A { template<typename T> struct a { using type = a; }; }; class B : A { template<typename T> using type = typename a<T>::type; }; int main(){ } 

RESULTS OF THE COMPLEX:

mingw64 / mingw-w64-x86_64-clang 3.9.1-3 (from MSYS2)

 $ clang++ -Wall test.cpp -o test.exe -std=c++14 

mingw64 / mingw-w64-x86_64-gcc 6.3.0-2 (from MSYS2)

 $ g++ -Wall test.cpp -o test.exe -std=c++14 

Both compilers accept without errors! Also, if you just move B::type to something like B::b::type , clang suddenly finds out that it should not access the private member, while g ++ compiles without problems:

 class A { template<typename T> struct a { using type = a; }; }; class B : A { template<typename T> struct b { using type = typename a<T>::type; }; }; int main(){ } 

COMPLEX RESULTS

mingw64 / mingw-w64-x86_64-clang 3.9.1-3 (from MSYS2)

 $ clang++ -Wall test.cpp -o test.exe -std=c++14 test.cpp:10:39: error: 'a' is a private member of 'A' struct b { using type = typename a<T>::type; }; ^ test.cpp:4:13: note: implicitly declared private here struct a { using type = a; }; ^ 1 error generated. 

mingw64 / mingw-w64-x86_64-gcc 6.3.0-2 (from MSYS2)

 $ g++ -Wall test.cpp -o test.exe -std=c++14 

My question is what causes this behavior when a derived class sometimes has access to its members of the base class, and sometimes not, and is this the expected behavior?

+7
c ++ metaprogramming c ++ 14
source share
1 answer

My question is what causes this behavior when a derived class sometimes has access to its members of the base class, and sometimes not, and is this the expected behavior?

Compiler error. There is a bunch of gcc bugs related to access control in templates, this is probably specifically addressed to # 41437 . The alias template clang # 15914 failed.

+9
source share

All Articles