静态C ++类成员何时初始化? [英] When are static C++ class members initialized?

查看:196
本文介绍了静态C ++类成员何时初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此似乎没有简单的答案,但是关于何时可以访问静态类字段可以安全地做出任何假设吗?

There appears to be no easy answer to this, but are there any assumptions that can be safely made about when a static class field can be accessed?

编辑:唯一安全的假设似乎是在程序启动之前初始化了所有静态变量(调用 main )。因此,只要我不从其他静态初始化代码中引用静态变量,就不必担心吗?

The only safe assumption seems to be that all statics are initialized before the program commences (call to main). So, as long as I don't reference statics from other static initialization code, I should have nothing to worry about?

推荐答案

该标准保证了两件事-在同一翻译单元(通常表示.cpp文件)中定义的对象将在它们的定义顺序(不是声明):

The standard guarantees two things - that objects defined in the same translation unit (usually it means .cpp file) are initialized in order of their definitions (not declarations):

3.6.2


具有静态存储持续时间(basic.stc.static)的对象的存储应在进行任何其他初始化之前进行零初始化(dcl.init)。零初始化和带有常量表达式的初始化统称为静态初始化;所有其他初始化是动态初始化。在使用动态表达式初始化之前,应使用常量表达式(expr.const)初始化具有静态存储持续时间的POD类型(basic.types)的对象。在同一个转换单元中的命名空间范围内定义并动态初始化的,具有静态存储持续时间的对象,应按照其定义在转换单元中出现的顺序进行初始化。

The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (basic.types) with static storage duration initialized with constant expressions (expr.const) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.

另一个保证的事情是,将在使用翻译单元中的任何对象或函数之前完成对翻译单元中静态对象的初始化:

The other guaranteed thing is that initialization of static objects from a translation unit will be done before use of any object or function from this translation unit:


由实现定义的是,是否在命名空间范围的对象的动态初始化(dcl.init,class.static,class.ctor,class.expl.init)之前完成主要。如果将初始化推迟到main的第一条语句之后的某个时间点,则应在首次使用与要初始化的对象相同的转换单元中定义的任何函数或对象之前进行。

It is implementation-defined whether or not the dynamic initialization (dcl.init, class.static, class.ctor, class.expl.init) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.

我没有其他保证(尤其是在实现中定义了在不同翻译单元中定义的对象的初始化顺序)。

Nothing else i guaranteed (especially order of initialization of objects defined in different translation units is implementation defined).

编辑
正如Suma的评论中指出的那样,还可以确保在输入 main 之前对它们进行初始化。

EDIT As pointed in Suma's comment, it is also guaranteed that they are initialized before main is entered.

这篇关于静态C ++类成员何时初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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