2017-03-16 169 views
7

我遇到了一个奇怪的情况,我的派生类能够访问其基类的私有成员,其中包含模板。派生类可以访问其基类的私有成员


考虑这个例子:

class A 
{ 
    template<typename T> 
    struct a { using type = a; }; 
}; 

class B : A 
{ 
    template<typename T> 
    using type = typename a<T>::type; 
}; 

int main(){ } 

编译结果:

mingw64 /的mingw-W64-x86_64的-铛3.9.1-3(从MSYS2)

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

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

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

两种编译器接受没有错误!此外,如果您只需要移动B::type成类似B::b::type铛突然意识到它不应该被访问的私有成员,而G ++没有问题,编译:

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(){ } 

编译结果

mingw64/mingw- W64-x86_64的-铛3.9.1-3(从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(从MSYS2)

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

我的问题是,是什么原因造成这种现象,其中一个派生类,有时可以访问其基类的成员,有时不能,而这是预期的行为?

回答

9

我的问题是,什么导致这种行为,其中派生类有时有权访问其基类的成员,有时不会,这是预期的行为?

一个编译器错误。有一堆gcc bugs与模板中的访问控制相关,这个可能是由#41437专门解决的。铛别名模板错误是#15914

相关问题