- 当
noexcept是标识符时, 它的作用是在函数后面声明一个函数是否会抛出异常.- 当
noexcept是函数时, 它的作用是检查一个函数是否会抛出异常.备注:
noexcept编译期完成声明和检查工作.
noexcept主要是解决的问题是减少运行时开销. 运行时开销指的是, 编译器需要为代码生成一些额外的代码用来包裹原始代码,当出现异常时可以抛出一些相关的堆栈stack unwinding错误信息, 这里面包含,错误位置, 错误原因, 调用顺序和层级路径等信息.
当使用noexcept声明一个函数不会抛出异常候, 编译器就不会去生成这些额外的代码, 直接的减小的生成文件的大小, 间接的优化了程序运行效率.
noexcept 标识符
noexcept 标识符有几种写法: noexcept、noexcept(true)、noexcept(false)、noexcept(expression)、throw() .
其中 noexcept 默认表示 noexcept(true).
当 noexcept 是 true 时表示函数不会抛出异常,
当 noexcept 是 false 时表示函数可能会抛出异常.
throw()表示函数可能会抛出异常, 不建议使用该写法, 应该使用 noexcept(false), 因为 C++20 放弃这种写法.
// noexcept 标识符
// noexcept 相当于 noexcept(true)
// 声明noexcept(true)之后, 将表示这个是不会报错的.
// 如果报错的话, 进程直接结束, 不会抛出异常信息.
void example() noexcept {
cout << "hello called" << endl;
}
noexcept 函数
noexcept 函数用来检查一个函数是否声明了 noexcept, 如果声明了noexcept(true)则返回true, 如果声明了noexcept(false)则返回false.
#include <iostream>
using std::cout;
using std::endl;
using std::boolalpha;
// noexcept 标识符
void foo() noexcept(true) {
throw 4;
}
// noexcept 标识符
void bar() noexcept(false) {
throw 4;
}
int main(void) {
// noexcept 函数
cout << boolalpha << noexcept(foo()) << endl; // true
cout << boolalpha << noexcept(bar()) << endl; // false
return 0;
}
noexcept 函数 还可以在常规函数中配合 noexcept(expression) 标识符 共同完成对其他函数是否声明了 noexcept 的检查.
#include <iostream>
using std::cout;
using std::endl;
using std::boolalpha;
struct foo {
int a;
void getFoo() noexcept(true) {
cout << "foo.getFoo called" << endl;
}
void getBar() noexcept(false) {
cout << "foo.getBar called" << endl;
}
};
template<typename T>
void example_true(T t) noexcept(noexcept(t.getFoo())) {
cout << "example called" << endl;
}
template<typename T>
void example_false(T t) noexcept(noexcept(t.getBar())) {
cout << "example called" << endl;
}
int main(void) {
foo x{};
cout << boolalpha << noexcept(example_true(x)) << endl; // true
cout << boolalpha << noexcept(example_false(x)) << endl; // false
return 0;
}
参考
C++标准库 第二版 侯捷翻 第三章 第25页
noexcept operator
noexcept specifier