默认情况下是否继承构造函数 noexcept(true)?
Are inheriting constructors noexcept(true) by default?
Here 我发现:
Inheriting constructors [...] are all noexcept(true) by default, unless they are required to call a function that is noexcept(false), in which case these functions are noexcept(false).
这是否意味着在下面的示例中继承的构造函数是noexcept(true)
,即使它已在基class中明确定义为noexcept(false)
,或者它被认为是本身作为一个函数,它是 noexcept(false) 被调用?
struct Base {
Base() noexcept(false) { }
};
struct Derived: public Base {
using Base::Base;
};
int main() {
Derived d;
}
继承的构造函数也将是 noexcept(false)
,因为正如您引用的那样,继承的构造函数默认为 noexcept(true)
unless they are required to call a function that is 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。
Here 我发现:
Inheriting constructors [...] are all noexcept(true) by default, unless they are required to call a function that is noexcept(false), in which case these functions are noexcept(false).
这是否意味着在下面的示例中继承的构造函数是noexcept(true)
,即使它已在基class中明确定义为noexcept(false)
,或者它被认为是本身作为一个函数,它是 noexcept(false) 被调用?
struct Base {
Base() noexcept(false) { }
};
struct Derived: public Base {
using Base::Base;
};
int main() {
Derived d;
}
继承的构造函数也将是 noexcept(false)
,因为正如您引用的那样,继承的构造函数默认为 noexcept(true)
unless they are required to call a function that is 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。