clang/g++ 与私有继承和使用声明的区别

clang/g++ difference with private inheritance and using declaration

本文关键字:声明 区别 继承 g++ clang      更新时间:2023-10-16

请考虑以下代码:

#include <iostream>
struct Params { };
template <class T>
struct Base
{
    int data() const { return 42; }
};
template <template <class> class D, class P>
struct Middle : private D<P> // must be 'public' for g++
{
};
struct Final : public Middle<Base,Params>
{
    using Base<Params>::data;
};

int main() {
    Final f;
    std::cout << f.data() << std::endl;
    return 0;
}

这段代码编译成功,并用 clang 打印42,并在 gcc 上给我编译时错误

'int Base::d ata() const [with T = Params]' 无法访问

在这种情况下,哪种实现更符合C++标准?

GCC 是正确的。[namespace.udecl]/17:

继承构造函数的访问规则在 12.9 中指定; 否则,使用声明中提到的名称的所有实例 应可访问。特别是,如果派生类使用 使用声明 访问基类的成员,成员名称 应可访问。

相关文章: