82 lines
3.7 KiB
Markdown
82 lines
3.7 KiB
Markdown
[basic.start.static]
|
||
|
||
# 6 Basics [[basic]](./#basic)
|
||
|
||
## 6.10 Program execution [[basic.exec]](basic.exec#basic.start.static)
|
||
|
||
### 6.10.3 Start and termination [[basic.start]](basic.start#static)
|
||
|
||
#### 6.10.3.2 Static initialization [basic.start.static]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7238)
|
||
|
||
Variables with static storage duration
|
||
are initialized as a consequence of program initiation[.](#1.sentence-1)
|
||
|
||
Variables with
|
||
thread storage duration are initialized as a consequence of thread execution[.](#1.sentence-2)
|
||
|
||
Within each of these phases of initiation, initialization occurs as follows[.](#1.sentence-3)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7246)
|
||
|
||
[*Constant initialization*](#def:constant_initialization "6.10.3.2 Static initialization [basic.start.static]") is performed
|
||
if a variable with static or thread storage duration
|
||
is constant-initialized ([[expr.const]](expr.const "7.7 Constant expressions"))[.](#2.sentence-1)
|
||
|
||
If constant initialization is not performed, a variable with static
|
||
storage duration ([[basic.stc.static]](basic.stc.static "6.8.6.2 Static storage duration")) or thread storage
|
||
duration ([[basic.stc.thread]](basic.stc.thread "6.8.6.3 Thread storage duration")) is zero-initialized ([[dcl.init]](dcl.init "9.5 Initializers"))[.](#2.sentence-2)
|
||
|
||
Together, zero-initialization and constant initialization are called[*static initialization*](#def:initialization,static "6.10.3.2 Static initialization [basic.start.static]");
|
||
all other initialization is [*dynamic initialization*](#def:initialization,dynamic "6.10.3.2 Static initialization [basic.start.static]")[.](#2.sentence-3)
|
||
|
||
All static initialization strongly happens before ([[intro.races]](intro.races "6.10.2.2 Data races"))
|
||
any dynamic initialization[.](#2.sentence-4)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
The dynamic initialization of non-block variables is described
|
||
in [[basic.start.dynamic]](basic.start.dynamic "6.10.3.3 Dynamic initialization of non-block variables"); that of static block variables is described
|
||
in [[stmt.dcl]](stmt.dcl "8.10 Declaration statement")[.](#2.sentence-5)
|
||
|
||
â *end note*]
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7266)
|
||
|
||
An implementation is permitted to perform the initialization of a
|
||
variable with static or thread storage duration as a static
|
||
initialization even if such initialization is not required to be done
|
||
statically, provided that
|
||
|
||
- [(3.1)](#3.1)
|
||
|
||
the dynamic version of the initialization does not change the
|
||
value of any other object of static or thread storage duration
|
||
prior to its initialization, and
|
||
|
||
- [(3.2)](#3.2)
|
||
|
||
the static version of the initialization produces the same value
|
||
in the initialized variable as would be produced by the dynamic
|
||
initialization if all variables not required to be initialized statically
|
||
were initialized dynamically[.](#3.sentence-1)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
As a consequence, if the initialization of an object obj1 refers to an
|
||
object obj2 potentially requiring dynamic initialization and defined
|
||
later in the same translation unit, it is unspecified whether the value of obj2 used
|
||
will be the value of the fully initialized obj2 (because obj2 was statically
|
||
initialized) or will be the value of obj2 merely zero-initialized[.](#3.sentence-2)
|
||
|
||
For example,inline double fd() { return 1.0; }extern double d1;double d2 = d1; // unspecified:// either statically initialized to 0.0 or// dynamically initialized to 0.0 if d1 is// dynamically initialized, or 1.0 otherwisedouble d1 = fd(); // either initialized statically or dynamically to 1.0
|
||
|
||
â *end note*]
|