C++ 静态存储周期(static storage duration)

时间:2014-05-22 11:12:17   收藏:0   阅读:372

拥有静态存储周期(static storage duration)的对象将会被一直保存到程序结束。


声明

存储类型说明符(static)用于声明对象拥有静态存储期(static storage duration)。


初始化

程序的启动阶段包括名字空间中静态对象的初始化过程。

非局部对象

  1. 拥有非局部静态对象应该在所有初始化之前先进行零初始化。零初始化和常量表达式初始化统称为静态初始化,其余的初始化为动态初始化。 
  2. 拥有常量表达式的POD类型的非局部静态对象的初始化应该在所有动态初始化之前进行。
  3. 同一编译单元的名字空间中静态对象的动态初始化过程按照它们的定义出现的顺序依次进行。对于多个翻译单元的静态对象,不能保证严格的初始化顺序,也无法来指定这种顺序。
  4. An implementation is permitted to perform the initialization of an object of namespace scope with static storage duration as a static initialization even if such initialization is not required to be done statically, provided that
    — the dynamic version of the initialization does not change the value of any other object of namespace scope with static storage duration prior to its initialization, and
    — the static version of the initialization produces the same value in the initialized object as would be produced by the dynamic initialization if all objects not required to be initialized statically were initialized dynamically.
  5. 如果非局部对象的构造或者析构抛出了未捕获的异常则调用 terminate.

局部对象

  1. 局部静态对象应该在所有初始化之前先进行零初始化。
  2. 拥有常量表达式的POD类型的局部静态对象的初始化应该其所在块第一次进入之前初始化。
  3. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope. Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization.
  4. 如果局部对象的初始化抛出异常则初始化未完成,下次进入其声明的时候将会再次尝试初始化。If control re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined.

销毁

程序的终止过程包含了静态对象的销毁。


PS:本文是根据C++ 2003标准整理而得,欢迎大家讨论

C++ 静态存储周期(static storage duration),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!