tmp/tmppapuwh53/{from.md → to.md}
RENAMED
|
@@ -1,141 +0,0 @@
|
|
| 1 |
-
### Initialization of non-local variables <a id="basic.start.init">[[basic.start.init]]</a>
|
| 2 |
-
|
| 3 |
-
There are two broad classes of named non-local variables: those with
|
| 4 |
-
static storage duration ([[basic.stc.static]]) and those with thread
|
| 5 |
-
storage duration ([[basic.stc.thread]]). Non-local variables with
|
| 6 |
-
static storage duration are initialized as a consequence of program
|
| 7 |
-
initiation. Non-local variables with thread storage duration are
|
| 8 |
-
initialized as a consequence of thread execution. Within each of these
|
| 9 |
-
phases of initiation, initialization occurs as follows.
|
| 10 |
-
|
| 11 |
-
Variables with static storage duration ([[basic.stc.static]]) or thread
|
| 12 |
-
storage duration ([[basic.stc.thread]]) shall be zero-initialized (
|
| 13 |
-
[[dcl.init]]) before any other initialization takes place. A *constant
|
| 14 |
-
initializer* for an object `o` is an expression that is a constant
|
| 15 |
-
expression, except that it may also invoke `constexpr` constructors for
|
| 16 |
-
`o` and its subobjects even if those objects are of non-literal class
|
| 17 |
-
types such a class may have a non-trivial destructor . *Constant
|
| 18 |
-
initialization* is performed:
|
| 19 |
-
|
| 20 |
-
- if each full-expression (including implicit conversions) that appears
|
| 21 |
-
in the initializer of a reference with static or thread storage
|
| 22 |
-
duration is a constant expression ([[expr.const]]) and the reference
|
| 23 |
-
is bound to an lvalue designating an object with static storage
|
| 24 |
-
duration, to a temporary (see [[class.temporary]]), or to a function;
|
| 25 |
-
- if an object with static or thread storage duration is initialized by
|
| 26 |
-
a constructor call, and if the initialization full-expression is a
|
| 27 |
-
constant initializer for the object;
|
| 28 |
-
- if an object with static or thread storage duration is not initialized
|
| 29 |
-
by a constructor call and if either the object is value-initialized or
|
| 30 |
-
every full-expression that appears in its initializer is a constant
|
| 31 |
-
expression.
|
| 32 |
-
|
| 33 |
-
Together, zero-initialization and constant initialization are called
|
| 34 |
-
*static initialization*; all other initialization is *dynamic
|
| 35 |
-
initialization*. Static initialization shall be performed before any
|
| 36 |
-
dynamic initialization takes place. Dynamic initialization of a
|
| 37 |
-
non-local variable with static storage duration is either ordered or
|
| 38 |
-
unordered. Definitions of explicitly specialized class template static
|
| 39 |
-
data members have ordered initialization. Other class template static
|
| 40 |
-
data members (i.e., implicitly or explicitly instantiated
|
| 41 |
-
specializations) have unordered initialization. Other non-local
|
| 42 |
-
variables with static storage duration have ordered initialization.
|
| 43 |
-
Variables with ordered initialization defined within a single
|
| 44 |
-
translation unit shall be initialized in the order of their definitions
|
| 45 |
-
in the translation unit. If a program starts a thread (
|
| 46 |
-
[[thread.threads]]), the subsequent initialization of a variable is
|
| 47 |
-
unsequenced with respect to the initialization of a variable defined in
|
| 48 |
-
a different translation unit. Otherwise, the initialization of a
|
| 49 |
-
variable is indeterminately sequenced with respect to the initialization
|
| 50 |
-
of a variable defined in a different translation unit. If a program
|
| 51 |
-
starts a thread, the subsequent unordered initialization of a variable
|
| 52 |
-
is unsequenced with respect to every other dynamic initialization.
|
| 53 |
-
Otherwise, the unordered initialization of a variable is indeterminately
|
| 54 |
-
sequenced with respect to every other dynamic initialization. This
|
| 55 |
-
definition permits initialization of a sequence of ordered variables
|
| 56 |
-
concurrently with another sequence. The initialization of local static
|
| 57 |
-
variables is described in [[stmt.dcl]].
|
| 58 |
-
|
| 59 |
-
An implementation is permitted to perform the initialization of a
|
| 60 |
-
non-local variable with static storage duration as a static
|
| 61 |
-
initialization even if such initialization is not required to be done
|
| 62 |
-
statically, provided that
|
| 63 |
-
|
| 64 |
-
- the dynamic version of the initialization does not change the value of
|
| 65 |
-
any other object of namespace scope prior to its initialization, and
|
| 66 |
-
- the static version of the initialization produces the same value in
|
| 67 |
-
the initialized variable as would be produced by the dynamic
|
| 68 |
-
initialization if all variables not required to be initialized
|
| 69 |
-
statically were initialized dynamically.
|
| 70 |
-
|
| 71 |
-
As a consequence, if the initialization of an object `obj1` refers to an
|
| 72 |
-
object `obj2` of namespace scope potentially requiring dynamic
|
| 73 |
-
initialization and defined later in the same translation unit, it is
|
| 74 |
-
unspecified whether the value of `obj2` used will be the value of the
|
| 75 |
-
fully initialized `obj2` (because `obj2` was statically initialized) or
|
| 76 |
-
will be the value of `obj2` merely zero-initialized. For example,
|
| 77 |
-
|
| 78 |
-
``` cpp
|
| 79 |
-
inline double fd() { return 1.0; }
|
| 80 |
-
extern double d1;
|
| 81 |
-
double d2 = d1; // unspecified:
|
| 82 |
-
// may be statically initialized to 0.0 or
|
| 83 |
-
// dynamically initialized to 0.0 if d1 is
|
| 84 |
-
// dynamically initialized, or 1.0 otherwise
|
| 85 |
-
double d1 = fd(); // may be initialized statically or dynamically to 1.0
|
| 86 |
-
```
|
| 87 |
-
|
| 88 |
-
It is *implementation-defined* whether the dynamic initialization of a
|
| 89 |
-
non-local variable with static storage duration is done before the first
|
| 90 |
-
statement of `main`. If the initialization is deferred to some point in
|
| 91 |
-
time after the first statement of `main`, it shall occur before the
|
| 92 |
-
first odr-use ([[basic.def.odr]]) of any function or variable defined
|
| 93 |
-
in the same translation unit as the variable to be initialized.[^11]
|
| 94 |
-
|
| 95 |
-
``` cpp
|
| 96 |
-
// - File 1 -
|
| 97 |
-
#include "a.h"
|
| 98 |
-
#include "b.h"
|
| 99 |
-
B b;
|
| 100 |
-
A::A(){
|
| 101 |
-
b.Use();
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
// - File 2 -
|
| 105 |
-
#include "a.h"
|
| 106 |
-
A a;
|
| 107 |
-
|
| 108 |
-
// - File 3 -
|
| 109 |
-
#include "a.h"
|
| 110 |
-
#include "b.h"
|
| 111 |
-
extern A a;
|
| 112 |
-
extern B b;
|
| 113 |
-
|
| 114 |
-
int main() {
|
| 115 |
-
a.Use();
|
| 116 |
-
b.Use();
|
| 117 |
-
}
|
| 118 |
-
```
|
| 119 |
-
|
| 120 |
-
It is implementation-defined whether either `a` or `b` is initialized
|
| 121 |
-
before `main` is entered or whether the initializations are delayed
|
| 122 |
-
until `a` is first odr-used in `main`. In particular, if `a` is
|
| 123 |
-
initialized before `main` is entered, it is not guaranteed that `b` will
|
| 124 |
-
be initialized before it is odr-used by the initialization of `a`, that
|
| 125 |
-
is, before `A::A` is called. If, however, `a` is initialized at some
|
| 126 |
-
point after the first statement of `main`, `b` will be initialized prior
|
| 127 |
-
to its use in `A::A`.
|
| 128 |
-
|
| 129 |
-
It is *implementation-defined* whether the dynamic initialization of a
|
| 130 |
-
non-local variable with static or thread storage duration is done before
|
| 131 |
-
the first statement of the initial function of the thread. If the
|
| 132 |
-
initialization is deferred to some point in time after the first
|
| 133 |
-
statement of the initial function of the thread, it shall occur before
|
| 134 |
-
the first odr-use ([[basic.def.odr]]) of any variable with thread
|
| 135 |
-
storage duration defined in the same translation unit as the variable to
|
| 136 |
-
be initialized.
|
| 137 |
-
|
| 138 |
-
If the initialization of a non-local variable with static or thread
|
| 139 |
-
storage duration exits via an exception, `std::terminate` is called (
|
| 140 |
-
[[except.terminate]]).
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|