gpt4 book ai didi

c++ - clang/g++ 与私有(private)继承和使用声明的区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:55 24 4
gpt4 key购买 nike

考虑以下代码:

#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;
}

此代码编译成功并打印42 clang并在 gcc 上给我编译时错误

'int Base::data() const [with T = Params]' is inaccessible

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

最佳答案

海湾合作委员会是正确的。 [命名空间.udecl]/17:

The access rules for inheriting constructors are specified in 12.9; otherwise all instances of the name mentioned in a using-declaration shall be accessible. In particular, if a derived class uses a using-declaration to access a member of a base class, the member name shall be accessible.

关于c++ - clang/g++ 与私有(private)继承和使用声明的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28223930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com