派生类能够访问其基类的私有成员

Dus*_*ger 7 c++ metaprogramming c++14

我遇到了一个奇怪的情况,我的派生类能够访问其基类的私有成员,其中涉及模板.


考虑这个例子:

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

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

int main(){ }
Run Code Online (Sandbox Code Playgroud)

编制结果:

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

$ clang++ -Wall test.cpp -o test.exe -std=c++14
Run Code Online (Sandbox Code Playgroud)

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

$ g++ -Wall test.cpp -o test.exe -std=c++14
Run Code Online (Sandbox Code Playgroud)

两个编译器都接受没有错误!另外,如果你只是B::type进入像B::b::typeclang 这样的东西突然意识到它不应该访问私有成员,而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(){ }
Run Code Online (Sandbox Code Playgroud)

编制结果

mingw64/mingw-w64-x86_64-clang 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.
Run Code Online (Sandbox Code Playgroud)

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

$ g++ -Wall test.cpp -o test.exe -std=c++14
Run Code Online (Sandbox Code Playgroud)

我的问题是,导致这种行为的原因是派生类有时可以访问其基类的成员,有时却没有,这是预期的行为吗?

Bar*_*rry 9

我的问题是,导致这种行为的原因是派生类有时可以访问其基类的成员,有时却没有,这是预期的行为吗?

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