gpt4 book ai didi

c++ - 默认情况下是否继承构造函数 noexcept(true)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:40 28 4
gpt4 key购买 nike

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),即使它已在基类中显式定义为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。

关于c++ - 默认情况下是否继承构造函数 noexcept(true)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35784391/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com