std::atexit

std::atexit

C++

语言

标准库头文件

自立与有宿主实现

具名要求

语言支持库

概念库 (C++20)

诊断库

工具库

字符串库

容器库

迭代器库

范围库 (C++20)

算法库

数值库

本地化库

输入/输出库

文件系统库 (C++17)

正则表达式库 (C++11)

原子操作库 (C++11)

线程支持库 (C++11)

技术规范

工具库

语言支持

类型支持(基本类型、 RTTI 、类型特征)

库功能特性测试宏 (C++20)

动态内存管理

程序工具

错误处理

协程支持 (C++20)

变参数函数

launder(C++17)

initializer_list(C++11)

source_location(C++20)

三路比较 (C++20)

three_way_comparablethree_way_comparable_with(C++20)(C++20)

strong_ordering(C++20)

weak_ordering(C++20)

partial_ordering(C++20)

common_comparison_category(C++20)

compare_three_way_result(C++20)

compare_three_way(C++20)

strong_order(C++20)

weak_order(C++20)

partial_order(C++20)

compare_strong_order_fallback(C++20)

compare_weak_order_fallback(C++20)

compare_partial_order_fallback(C++20)

is_eqis_neqis_ltis_lteqis_gtis_gteq(C++20)(C++20)(C++20)(C++20)(C++20)(C++20)

通用工具

日期和时间

函数对象

格式化库 (C++20)

bitset

hash(C++11)

integer_sequence(C++14)

关系运算符 (C++20 中弃用)

rel_ops::operator!=rel_ops::operator>rel_ops::operator<=rel_ops::operator>=

整数比较函数

cmp_equalcmp_not_equalcmp_lesscmp_greatercmp_less_thancmp_greater_than(C++20)(C++20)(C++20)(C++20)(C++20)(C++20)

in_range(C++20)

swap 与类型运算

swap

ranges::swap(C++20)

exchange(C++14)

declval(C++11)

forward(C++11)

move(C++11)

move_if_noexcept(C++11)

as_const(C++17)

常用词汇类型

pair

tuple(C++11)

apply(C++17)

make_from_tuple(C++17)

optional(C++17)

any(C++17)

variant(C++17)

初等字符串转换

to_chars(C++17)

from_chars(C++17)

chars_format(C++17)

程序支持工具

程序终止

abort

exit

quick_exit(C++11)

_Exit(C++11)

atexit

at_quick_exit(C++11)

EXIT_SUCCESSEXIT_FAILURE

与环境交流

system

getenv

信号

signal

raise

sig_atomic_t

SIG_DFLSIG_IGN

SIG_ERR

信号类型

SIGABRTSIGFPESIGILLSIGINTSIGSEGVSIGTERM

非局部跳转

setjmp

longjmp

类型

jmp_buf

定义于头文件

(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 的调用。同样的规则应用于静态对象构造函数和到 atexit 的调用:见 std::exit 。

(C++11 前)

函数可能与拥有静态存储期的对象的析构函数,或彼此间并发调用,这保持保证若 A 的注册先序于 B 的注册,则对 B 的调用先序于对 A 的调用,同样的规则应用于静态对象构造函数和到 atexit 的调用:见 std::exit 。

(C++11 起)

可以注册同一函数多于一次。

若函数通过异常退出,则调用 std::terminate 。

atexit 是线程安全的:从数个线程调用函数不引入数据竞争。

保证实现支持注册至少 32 个函数。准确限制是实现定义的。

参数

func

-

指向正常程序终止时要调用的函数的指针

返回值

若注册成功则为 ​0​ ,否则为非零值。

注解

二个重载有别,因为参数 func 类型有别(语言链接是其类型的一部分)。

示例

运行此代码

#include

#include

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) || (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

参阅

abort

导致非正常的程序终止(不进行清理) (函数)

exit

导致正常的程序终止并进行清理 (函数)

quick_exit(C++11)

导致快速程序终止,不进行完全的清理 (函数)

at_quick_exit(C++11)

注册将于调用 quick_exit 时被调用的函数 (函数)

相关文章

bat365官网登录下载
显示已锁定电源按钮怎么解除