为什么我不能继承虚拟基的构造函数?

Why can't I inherit the constructors of a virtual base?

本文关键字:构造函数 虚拟 不能 继承 为什么      更新时间:2023-10-16

我正在尝试使用g++4.9.0编译以下简单代码:

struct A {
    explicit A(int x) { }
};
struct B : public virtual A {
    using A::A;
};
int main(int argc, char** argv) {
    B b(0);
    return 0;
}

但我得到以下错误:

$ g++ -std=c++11 main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:10:10: error: use of deleted function ‘B::B(int)’
     B b(0);
          ^
main.cpp:6:14: note: ‘B::B(int)’ is implicitly deleted because the default definition would be ill-formed:
     using A::A;
              ^
main.cpp:6:14: error: no matching function for call to ‘A::A()’
main.cpp:6:14: note: candidates are:
main.cpp:2:14: note: A::A(int)
     explicit A(int x) { }
              ^
main.cpp:2:14: note:   candidate expects 1 argument, 0 provided
main.cpp:1:8: note: constexpr A::A(const A&)
 struct A {
        ^
main.cpp:1:8: note:   candidate expects 1 argument, 0 provided
main.cpp:1:8: note: constexpr A::A(A&&)
main.cpp:1:8: note:   candidate expects 1 argument, 0 provided

我做错什么了吗?这是编译器错误吗?

这是一个GCC错误。§7.3.3[名称空间.udcl]/p3要求

在用作成员声明的using声明嵌套名称说明符应命名所定义类的基类。如果这样的using声明命名构造函数,则嵌套名称说明符应命名所定义类的直接基类。。。

AB的直接基,因此允许using A::A;

该标准规定(§12.9【class.inctor】/p8):

隐式定义的继承构造函数执行由用户编写的类的初始化具有mem初始值设定项列表的类的inline构造函数只有mem初始值设定项具有命名基的mem初始值设置项id在using声明的嵌套名称说明符中表示的类和下面指定的表达式列表,其中函数体中的复合语句为空(12.6.2)。如果用户编写的构造函数格式不正确,程序不正规。表达式列表中的每个表达式的形式static_cast<T&&>(p),其中p是对应的构造函数参数,并且Tp的声明类型。

因此,相应的用户编写的构造函数是

B::B(int x) : A(static_cast<int&&>(x)) { }

其形成良好。

它似乎是用clang编译的,但不是用gcc编译的(至少在我的机器上是这样)。然而,即使使用了gcc,如果您只需向基类中添加一个explicit A()构造函数,代码也可以编译并正常工作。

EDIT显然,正如@T.C.在评论中指出的,这是GCC中的一个已知错误。