std::atexit

std::atexit

C++

编译器支持

自由(freestanding)与宿主(hosted)

语言

标准库

标准库头文件

具名要求

特性测试宏 (C++20)

语言支持库

概念库 (C++20)

诊断库

内存管理库

元编程库 (C++11)

通用工具库

容器库

迭代器库

范围库 (C++20)

算法库

字符串库

文本处理库

数值库

日期和时间库

输入/输出库

文件系统库 (C++17)

并发支持库 (C++11)

执行控制库 (C++26)

技术规范

符号索引

外部库

[编辑] 工具库

语言支持

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

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

程序工具

变参函数

initializer_list(C++11)

is_constant_evaluated(C++20)

is_within_lifetime(C++26)

source_location(C++20)

协程支持 (C++20)

契约支持 (C++26)

三路比较

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_ltis_lteq(C++20)(C++20)(C++20)

is_neqis_gtis_gteq(C++20)(C++20)(C++20)

通用工具

函数对象

位操作 (C++20)

bitset

hash(C++11)

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

rel_ops::operator!=rel_ops::operator>

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

整数比较函数

cmp_equalcmp_lesscmp_less_than(C++20)(C++20)(C++20)

cmp_not_equalcmp_greatercmp_greater_than(C++20)(C++20)(C++20)

in_range(C++20)

交换与类型操作

swap

ranges::swap(C++20)

exchange(C++14)

declval(C++11)

to_underlying(C++23)

forward(C++11)

forward_like(C++23)

move(C++11)

move_if_noexcept(C++11)

as_const(C++17)

常用词汇类型

pair

tuple(C++11)

optional(C++17)

any(C++17)

variant(C++17)

tuple_size(C++11)

tuple_element(C++11)

apply(C++17)

make_from_tuple(C++17)

expected(C++23)

[编辑] 程序支持实用程序

程序终止

abort

exit

quick_exit(C++11)

_Exit(C++11)

atexit

at_quick_exit(C++11)

EXIT_SUCCESSEXIT_FAILURE

不可达控制流

unreachable(C++23)

与环境通信

system

getenv

信号

signal

raise

sig_atomic_t

SIG_DFLSIG_IGN

SIG_ERR

信号类型

SIGABRTSIGFPESIGILL

SIGINTSIGSEGVSIGTERM

非局部跳转

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 /* c-atexit-handler */ = void();

extern "C++" using /* 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 个函数的注册。确切的限制是实现定义的。

目录

1 参数

2 返回值

3 注意

4 示例

5 另请参阅

[编辑] 参数

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

{

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)

注册一个函数,在调用 std::quick_exit 时被调用 (函数) [编辑]

C 文档 关于 atexit

相关推荐