相关疑难解决方法(0)

noexcept,继承构造函数和无效使用实际完成的不完整类型

我不确定这是GCC编译器的错误还是预期的行为noexcept.
请考虑以下示例:

struct B {
    B(int) noexcept { }
    virtual void f() = 0;
};

struct D: public B {
    using B::B;
    D() noexcept(noexcept(D{42})): B{42} { }
    void f() override { }
};

int main() {
    B *b = new D{};
}
Run Code Online (Sandbox Code Playgroud)

如果noexcept删除它,它编译.
无论如何,正如在例子中,我从GCC v5.3.1得到了这个错误:

test.cpp:8:31: error: invalid use of incomplete type ‘struct D’
     D() noexcept(noexcept(D{42})): B{42} { }
                               ^
Run Code Online (Sandbox Code Playgroud)

据我所知,struct D不是一个不完整的类型,但继承构造函数涉及到语句,看起来编译器实际上考虑的是基本结构的完整性而B不是D.

这是预期的行为还是合法代码?

为清楚起见:

c++ language-lawyer noexcept c++11 inheriting-constructors

12
推荐指数
1
解决办法
442
查看次数