示例静态和动态初始化 [英] Example Static and Dynamic Initialization

查看:96
本文介绍了示例静态和动态初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个例子,其中变量以静态和动态方式初始化。也就是说

Is there an example in which variable is initialized both in static and dynamic way. I.e. as said the spec.


静态初始化应在任何动态
初始化之前执行。

Static initialization shall be performed before any dynamic initialization takes place.

我假设变量可以在同一程序中以静态和动态方式初始化。虽然,我认为,初始化是定义一个初始值。也就是说变量,初始化一次,不能被初始化。

I assume that it is possible that variable can be initialized in static and dynamic way in the same program. Although, I think, that initialization is definition of an initial value. I.e. variable, initialized once, cannot be initialized anymore.

推荐答案

Sure:static initialization,任何用户代码都会运行。

Sure: static initialization, which fills all objects with zero, takes place before any user code runs.

这些零在执行其他初始化程序时可能是可见的。了解静态初始化订单Fiasco

Those zeros might be visible during the execution of other initializers. Read about the "Static Initialization Order Fiasco"

请注意对象不会被构造,直到动态初始化完成后,你必须遵循生命周期规则。 [basic.life] 在标准中说:

Note that objects are not deemed constructed until after dynamic initialization completes, and you have to follow the lifetime rules. [basic.life] in the Standard says:


是对象的运行时属性。如果对象是类或聚合类型,并且它的一个成员由除了琐碎的默认构造函数之外的构造函数初始化,则该对象被称为具有非平凡的初始化。 [注意:通过一个简单的复制/移动构造函数的初始化是非平凡的初始化。 - end note]类型 T 的对象的生命周期开始于:

The lifetime of an object is a runtime property of the object. An object is said to have non-trivial initialization if it is of a class or aggregate type and it or one of its members is initialized by a constructor other than a trivial default constructor. [ Note: initialization by a trivial copy/move constructor is non-trivial initialization. — end note ] The lifetime of an object of type T begins when:


  • 获得具有对于类型 T 的正确对齐和大小的存储,并且如果对象具有不重要的初始化,则

  • 完成。

  • storage with the proper alignment and size for type T is obtained, and
  • if the object has non-trivial initialization, its initialization is complete.


本国际标准中归属于对象的属性仅在给定对象的生命周期内适用于给定对象。 [注意:特别是,在对象的生命周期开始之前和生命周期结束后,对对象的使用存在重大限制,如下所述,在12.6.2和12.7中。此外,正在构建和销毁的对象的行为可能与其生命周期已开始但未结束的对象的行为不同。 12.6.2和12.7描述了在建造和销毁阶段期间物体的行为。 - end note]

The properties ascribed to objects throughout this International Standard apply for a given object only during its lifetime. [ Note: In particular, before the lifetime of an object starts and after its lifetime ends there are significant restrictions on the use of the object, as described below, in 12.6.2 and in 12.7. Also, the behavior of an object under construction and destruction might not be the same as the behavior of an object whose lifetime has started and not ended. 12.6.2 and 12.7 describe the behavior of objects during the construction and destruction phases. — end note ]


类似地,在对象的生存期开始之前但是在对象将占据的存储已经被分配之后,或者在对象的生命周期结束之后并且在对象被占用的存储被重新使用或释放​​之前,任何引用可以使用原始对象,但仅以有限的方式。对于正在建设或销毁的对象,见12.7。否则,这样的glvalue指的是分配的存储(3.7.4.2),并且使用不依赖于其值的glvalue的属性是明确定义的。如果:

Similarly, before the lifetime of an object has started but after the storage which the object will occupy has been allocated or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any glvalue that refers to the original object may be used but only in limited ways. For an object under construction or destruction, see 12.7. Otherwise, such a glvalue refers to allocated storage (3.7.4.2), and using the properties of the glvalue that do not depend on its value is well-defined. The program has undefined behavior if:


  • 程序具有未定义的行为,则将左值转换为右值转换(4.1)
  • glvalue用于访问非静态数据成员或调用对象的非静态成员函数,或

  • glvalue绑定到引用到虚拟基类(8.5.3)或

  • ,glvalue用作 dynamic_cast )或作为 typeid 的操作数。

  • an lvalue-to-rvalue conversion (4.1) is applied to such a glvalue,
  • the glvalue is used to access a non-static data member or call a non-static member function of the object, or
  • the glvalue is bound to a reference to a virtual base class (8.5.3), or
  • the glvalue is used as the operand of a dynamic_cast (5.2.7) or as the operand of typeid.

因此,访问非原始类型的对象的(归零)内容很容易导致未定义的行为。

So, accessing the (zeroed) content of objects of non-primitive type can easily lead to undefined behavior.

这篇关于示例静态和动态初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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