clang/g++ 与私有继承和使用声明的区别
clang/g++ difference with private inheritance and using declaration
请考虑以下代码:
#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 中指定; 否则,使用声明中提到的名称的所有实例 应可访问。特别是,如果派生类使用 使用声明 访问基类的成员,成员名称 应可访问。
相关文章:
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 在以下声明中使用指针有什么区别?
- 在类内和类外的定义处执行类转发声明是否有区别
- 类型测试对象的动态数组的这两个声明之间的区别?
- 使用 std::move 将参数传递给函数,如果该参数声明为按值传递或使用移动操作数 &&,是否有区别?
- 有没有办法声明一个公共静态常量,该常量将使用 constexpr 在源文件中定义(有什么区别)?
- 如果我提前将参数声明为变量而不是将它们内联写入函数调用,那有什么区别(在内存方面)?
- 初始化的多维数组,用声明初始化和声明后初始化有什么区别
- 以下声明之间的区别
- 将全局声明为类声明语句的一部分与使用单独的语句声明全局之间是否有区别
- 指针声明和间接寻址之间的区别
- C++ - 在线程中使用时,将函数的返回值声明为 void/void* 之间有什么区别吗?
- 声明静态对象和指向静态对象的指针之间的区别
- 恒定声明全局或在使用它的函数中声明恒定声明之间有什么区别?
- 在 C++ 中:在函数中初始化变量与在函数标头中声明变量有什么区别?
- C++函数声明和对象初始化之间的区别
- 在声明末尾或 main 中声明结构或类实例之间的区别
- 在构造函数中创建数组与在 C++ 中创建声明之间的区别
- 从函数返回时,自动和声明类(自动)有什么区别?
- C++中变量的声明和定义之间有什么区别