Files
2025-10-25 03:02:53 +03:00

82 lines
3.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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.2Static initialization[basic.start.static]") is performed
if a variable with static or thread storage duration
is constant-initialized ([[expr.const]](expr.const "7.7Constant 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.2Static storage duration")) or thread storage
duration ([[basic.stc.thread]](basic.stc.thread "6.8.6.3Thread storage duration")) is zero-initialized ([[dcl.init]](dcl.init "9.5Initializers"))[.](#2.sentence-2)
Together, zero-initialization and constant initialization are called[*static initialization*](#def:initialization,static "6.10.3.2Static initialization[basic.start.static]");
all other initialization is [*dynamic initialization*](#def:initialization,dynamic "6.10.3.2Static initialization[basic.start.static]")[.](#2.sentence-3)
All static initialization strongly happens before ([[intro.races]](intro.races "6.10.2.2Data 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.3Dynamic initialization of non-block variables"); that of static block variables is described
in [[stmt.dcl]](stmt.dcl "8.10Declaration 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*]