Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';我是否继承虚拟基的构造函数?_C++_Gcc4.9 - Fatal编程技术网

C++ 为什么可以';我是否继承虚拟基的构造函数?

C++ 为什么可以';我是否继承虚拟基的构造函数?,c++,gcc4.9,C++,Gcc4.9,我尝试使用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,

我尝试使用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(至少在我的机器上是这样)。但是,即使使用gcc,如果您只是简单地向基类添加一个
显式的a()
构造函数,就可以使用

编辑显然,正如@T.C.在评论中指出的,这是GCC中的一个。§7.3.3[名称空间.udecl]/p3要求

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

A
B
的直接基础,因此
使用A::A是允许的

本标准规定(§12.9[类别inhctor]/p8):

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

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

B::B(int x) : A(static_cast<int&&>(x)) { }
B::B(intx):A(static_cast(x)){
格式正确。

hm,不是GCC这是GCC错误。因此相关的是(实际上是GCC文件报告的来源):