构造函数尝试调用复制构造函数,虚拟继承

Constructor tries to call copy constructor, virtual inheritance

本文关键字:构造函数 虚拟 继承 调用 复制      更新时间:2023-10-16

我很难表达一个合适的标题。

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都接受代码。