构造函数尝试调用拷贝构造函数,虚继承
Constructor tries to call copy constructor, virtual inheritance
我很难说出一个合适的标题。
struct Base
{
Base(int) {}
virtual ~Base()=default;
};
struct Derived: virtual public Base
{
Derived(float, int): Base{1} {}
Derived(Derived const&)=delete;
~Derived()=default;
};
struct Comp: private Derived
{
Comp(): Base{1}, Derived{1.0f, 1} {}
};
这给出了一个编译器错误:
x.cc: In constructor ‘Comp::Comp()’:
x.cc:16:34: error: use of deleted function ‘Derived::Derived(const Derived&)’
Comp(): Base{1}, Derived{1.0f, 1} {}
^
x.cc:10:2: note: declared here
Derived(Derived const&)=delete;
^~~~~~~
为什么要在这里请求拷贝构造函数?当我摆脱虚拟继承时,问题就消失了(因此 Comp
的初始化列表中的 Base(int)
调用)。
这是在 gcc version 6.2.1 20161124 (Debian 6.2.1-5)
.
这似乎是 GCC 中的错误。 Clang 和 GCC 7 accept the code.
我很难说出一个合适的标题。
struct Base
{
Base(int) {}
virtual ~Base()=default;
};
struct Derived: virtual public Base
{
Derived(float, int): Base{1} {}
Derived(Derived const&)=delete;
~Derived()=default;
};
struct Comp: private Derived
{
Comp(): Base{1}, Derived{1.0f, 1} {}
};
这给出了一个编译器错误:
x.cc: In constructor ‘Comp::Comp()’:
x.cc:16:34: error: use of deleted function ‘Derived::Derived(const Derived&)’
Comp(): Base{1}, Derived{1.0f, 1} {}
^
x.cc:10:2: note: declared here
Derived(Derived const&)=delete;
^~~~~~~
为什么要在这里请求拷贝构造函数?当我摆脱虚拟继承时,问题就消失了(因此 Comp
的初始化列表中的 Base(int)
调用)。
这是在 gcc version 6.2.1 20161124 (Debian 6.2.1-5)
.
这似乎是 GCC 中的错误。 Clang 和 GCC 7 accept the code.