when是一个函数使用atexit()注册 [英] When is a function registered with atexit() called

查看:175
本文介绍了when是一个函数使用atexit()注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果在 atexit()注册的函数在全局变量被销毁之前或之后被调用。这是由标准还是实现定义的?

I want to know if functions registered with atexit() are called before or after global variables are destroyed. Is this specified by the standard or implementation defined?

推荐答案

它是明确定义的,使用 atexit()

It is well-defined, and depends on whether the object in question was constructed before or after the function got registered using atexit():

> 3.6.3终止

3。如果具有静态存储持续时间的对象的初始化完成,调用 std :: atexit (参见< cstdlib> ,18.5) code> std :: atexit 在调用对象的析构函数之前进行排序。如果在具有静态存储持续时间的对象的初始化完成之前对 std :: atexit 的调用进行排序,则在调用之前对对象的析构函数的调用到传递给 std :: atexit 的函数。如果在对 std :: atexit 的另一个调用之前对 std :: atexit 的调用进行排序,传递给第二个 std :: atexit 调用在调用函数之前先传递给第一个 std :: atexit call。

3. If the completion of the initialization of an object with static storage duration is sequenced before a call to std::atexit (see <cstdlib>, 18.5), the call to the function passed to std::atexit is sequenced before the call to the destructor for the object. If a call to std::atexit is sequenced before the completion of the initialization of an object with static storage duration, the call to the destructor for the object is sequenced before the call to the function passed to std::atexit. If a call to std::atexit is sequenced before another call to std::atexit, the call to the function passed to the second std::atexit call is sequenced before the call to the function passed to the first std::atexit call.

我的外行人对上述的解释是,在在调用 handler()之后,调用 c> c $ c> atexit(handler)我相信有微妙之处,但这似乎是基本的原则。

My layman's interpretation of the above is that stuff that got constructed before you called atexit(handler) gets destroyed after handler() is called, and vice versa. I am sure there are subtleties, but this seems to be the basic principle.

这篇关于when是一个函数使用atexit()注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆