C++ 参考手册
- C++11
 - C++14
 - C++17
 - C++20
 - C++ 编译器支持情况表
 - 独立与宿主实现
 - C++ 语言
 - C++ 关键词
 - 预处理器
 - C++ 标准库头文件
 - 具名要求
 - 功能特性测试 (C++20)
 - 工具库
 - 程序支持工具
 - std::longjmp
 - std::_Exit
 - EXIT_SUCCESS, EXIT_FAILURE
 - std::getenv
 - std::signal
 - std::sig_atomic_t
 - SIG_DFL, SIG_IGN
 - std::abort
 - std::atexit
 - std::exit
 - std::at_quick_exit
 - std::quick_exit
 - setjmp
 - std::system
 - std::raise
 - SIG_ERR
 - SIGTERM, SIGSEGV, SIGINT, SIGILL, SIGABRT, SIGFPE
 - std::jmp_buf
 - std::apply
 - 库特性测试宏 (C++20)
 - std::initializer_list
 - 函数对象
 - std::hash
 - std::pair
 - std::tuple
 - std::optional
 - std::any
 - std::variant
 - 格式化库 (C++20)
 - std::integer_sequence
 - std::exchange
 - std::make_from_tuple
 - std::launder
 - std::to_chars
 - std::from_chars
 - std::as_const
 - std::source_location
 - 变参数函数
 - std::bitset
 - std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
 - std::in_range
 - std::declval
 - std::forward
 - std::move
 - std::move_if_noexcept
 - std::chars_format
 - std::piecewise_construct_t
 - std::piecewise_construct
 - std::in_place, std::in_place_type, std::in_place_index, std::in_place_t, std::in_place_type_t, std::in_place_index_t
 - 注释
 - 类型支持(基本类型、RTTI、类型特性)
 - 概念库 (C++20)
 - 错误处理
 - 动态内存管理
 - 日期和时间工具
 - 字符串库
 - 容器库
 - 迭代器库
 - 范围库 (C++20)
 - 算法库
 - 数值库
 - 输入/输出库
 - 文件系统库
 - 本地化库
 - 正则表达式库
 - 原子操作库
 - 线程支持库
 - 实验性 C++ 特性
 - 有用的资源
 - 索引
 - std 符号索引
 - 协程支持 (C++20)
 - C++ 关键词
 
std::atexit
|   定义于头文件  <cstdlib>
  | 
||
| (1) | ||
|   int atexit( /*c-atexit-handler*/* func ); int atexit( /*atexit-handler*/* func );  | 
(C++11 前) | |
|   int atexit( /*c-atexit-handler*/* func ) noexcept; int atexit( /*atexit-handler*/* func ) noexcept;  | 
(C++11 起) | |
|   extern "C++" using /*atexit-handler*/ = void(); // 仅为说明 extern "C" using /*c-atexit-handler*/ = void(); // 仅为说明  | 
(2) | |
注册 func 所指向的函数,使得在正常程序中止(通过 std::exit() 或从 main 函数返回)时调用它。
| 
 可能在静态对象析构期间以逆序调用函数:若 A 先于 B 被注册,则对 B 的调用先进行于对 A 的调用。同样的规则应用于静态对象构造函数和到   | 
(C++11 前) | 
| 
 函数可能与拥有静态存储期的对象的析构函数,或彼此间并发调用,这保持保证若 A 的注册先序于 B 的注册,则对 B 的调用先序于对 A 的调用,同样的规则应用于静态对象构造函数和到   | 
(C++11 起) | 
可以注册同一函数多于一次。
若函数通过异常退出,则调用 std::terminate 。
atexit 是线程安全的:从数个线程调用函数不引入数据竞争。
保证实现支持注册至少 32 个函数。准确限制是实现定义的。
参数
| func | - | 指向正常程序终止时要调用的函数的指针 | 
返回值
若注册成功则为 0 ,否则为非零值。
注意
二个重载有别,因为参数 func 类型有别(语言链接是其类型的一部分)。
示例
运行此代码
#include <iostream> #include <cstdlib> void atexit_handler_1() { std::cout << "at exit #1\n"; } void atexit_handler_2() { std::cout << "at exit #2\n"; } int main() { const int result_1 = std::atexit(atexit_handler_1); const int result_2 = std::atexit(atexit_handler_2); if ((result_1 != 0) or (result_2 != 0)) { std::cerr << "Registration failed\n"; return EXIT_FAILURE; } std::cout << "returning from main\n"; return EXIT_SUCCESS; }
输出:
returning from main at exit #2 at exit #1
参阅
|   导致非正常的程序终止(不进行清理)  (函数)  | |
|   导致正常的程序终止并进行清理  (函数)  | |
|    (C++11)  | 
  导致快速程序终止,不进行完全的清理  (函数)  | 
|    (C++11)  | 
  注册将于调用 quick_exit 时被调用的函数  (函数)  |