std::quick_exit
参考 zh.cppreference.net
| 定义于头文件 <cstdlib>
|
||
[[noreturn]] void quick_exit( int exit_code ) noexcept;
|
(C++11 起) | |
导致程序正常终止,但不会完成资源的完全清理。
传递给 std::at_quick_exit 的函数以其注册顺序的逆序被调用。如果任何函数中尝试传播异常,则会调用 std::terminate。在调用已注册的函数后,会调用 std::_Exit(exit_code)。
传递给 std::atexit 的函数不会被调用。
参数
| exit_code | - | 程序的退出状态 |
示例
运行此代码
#include <cstdlib> #include <iostream> template<int N> void quick_exit_handler() { std::cout << "quick_exit handler #" << N << std::endl; // flush is intended } void at_exit_handler() { std::cout << "at_exit handler\n"; } int main() { if (std::at_quick_exit(quick_exit_handler<1>) || std::at_quick_exit(quick_exit_handler<2>)) { std::cerr << "Registration failed\n"; return EXIT_FAILURE; } std::atexit(at_exit_handler); // the handler will not be called struct R { ~R() { std::cout << "destructor\n"; } } resource; /*...*/ std::quick_exit(EXIT_SUCCESS); std::cout << "This statement is unreachable...\n"; }
输出:
quick_exit handler #2 quick_exit handler #1
参见
|
导致程序异常终止(不进行清理操作)
(function) |
|
|
导致程序正常终止并执行清理操作
(function) |
|
|
注册在调用
std::exit()
时要执行的函数
(function) |
|
|
(C++11)
|
注册在调用
std::quick_exit
时要执行的函数
(function) |
|
C documentation
for
quick_exit
|
|