继承虚拟基础的构造函数 类
Inheriting constructors of virtual base classes
虚拟基 classes 在最派生的 class 中初始化,所以我的猜测是继承基 class 的构造函数也应该有效:
struct base {
base(int) {}
};
struct derived: virtual base {
using base::base;
};
derived d(0);
然而,这无法用 GCC 5.2.0 编译,它试图找到 base::base()
,但在 Clang 3.6.2 上工作正常。这是 GCC 中的错误吗?
这是 gcc 错误 58751
“[C++11] 继承构造函数不能与虚拟继承一起正常工作”
(又名:63339
"using constructors" from virtual bases are implicitly deleted"):
来自58751描述:
In the document N2540 it states that:
Typically, inheriting constructor definitions for classes with virtual bases will be ill-formed, unless the virtual base supports default initialization, or the virtual base is a direct base, and named as the base forwarded-to. Likewise, all data members and other direct bases must support default initialization, or any attempt to use a inheriting constructor will be ill-formed. Note: ill-formed when used, not declared.
Hence, the case of virtual bases is explicitly considered by the committee and thus should be implemented.
从错误报告中借用的解决方法:
struct base {
base() = default; // <--- add this
base(int) {}
};
根据错误报告,在这种情况下,构造函数 base::base(int)
由隐式生成的构造函数 derived::derived(int)
调用。
我已经检查过 your code does not compile. But this 会调用 base::base(int)
构造函数。
虚拟基 classes 在最派生的 class 中初始化,所以我的猜测是继承基 class 的构造函数也应该有效:
struct base {
base(int) {}
};
struct derived: virtual base {
using base::base;
};
derived d(0);
然而,这无法用 GCC 5.2.0 编译,它试图找到 base::base()
,但在 Clang 3.6.2 上工作正常。这是 GCC 中的错误吗?
这是 gcc 错误 58751 “[C++11] 继承构造函数不能与虚拟继承一起正常工作” (又名:63339 "using constructors" from virtual bases are implicitly deleted"):
来自58751描述:
In the document N2540 it states that:
Typically, inheriting constructor definitions for classes with virtual bases will be ill-formed, unless the virtual base supports default initialization, or the virtual base is a direct base, and named as the base forwarded-to. Likewise, all data members and other direct bases must support default initialization, or any attempt to use a inheriting constructor will be ill-formed. Note: ill-formed when used, not declared.
Hence, the case of virtual bases is explicitly considered by the committee and thus should be implemented.
从错误报告中借用的解决方法:
struct base {
base() = default; // <--- add this
base(int) {}
};
根据错误报告,在这种情况下,构造函数 base::base(int)
由隐式生成的构造函数 derived::derived(int)
调用。
我已经检查过 your code does not compile. But this 会调用 base::base(int)
构造函数。