
[英]noexcept, inheriting constructors and the invalid use of an incomplete type that is actually complete
[英]Are inheriting constructors noexcept(true) by default?
在这里我发现:
默认情况下,继承构造函数都是noexcept(true),除非它们需要调用noexcept(false)函数,在这种情况下这些函数是noexcept(false)。
这是否意味着在下面的示例中,继承的构造函数是noexcept(true)
,即使它已在基类中显式定义为noexcept(false)
,或者它本身被视为noexcept(false)的函数叫做?
struct Base {
Base() noexcept(false) { }
};
struct Derived: public Base {
using Base::Base;
};
int main() {
Derived d;
}
继承的构造函数也将是noexcept(false)
因为你引用的是一个继承的构造函数,默认情况下是noexcept(true)
除非他们需要调用noexcept函数(false)
当Derived
构造函数运行时,它也将调用Base
构造函数,该构造函数为noexcept(false)
,因此, Derived
构造函数也将是noexcept(false)
。
以下证明了这一点。
#include <iostream>
struct Base {
Base() noexcept(false) { }
};
struct Derived: public Base {
using Base::Base;
};
int main() {
std::cout << noexcept(Derived());
}
输出0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.