static Variablen in Funktionen. Wann findet die Initialisierung statt?
-
Hallo.
void foo() { static bool initialized = false; }
Wann wird die Variable initialisiert? Beim Programmstart oder wenn die Funktion das erste mal betreten wird?
-
Beim ersten Aufruf (würde ich sagen).
-
Nabend,
der C++ Draft sagt folgendes:
3.6.2 Initialization of non-local objects [basic.start.init]
1 The storage for objects with static storage duration
(_basic.stc.static_) shall be zero-initialized (_dcl.init_) before any
other initialization takes place. Objects of POD types
(_basic.types_) with static storage duration initialized with constant
expressions (_expr.const_) shall be initialized before any dynamic
initialization takes place. Objects of namespace scope with static
storage duration defined in the same translation unit and dynamically
initialized shall be initialized in the order in which their defini-
tion appears in the translation unit.
...Weiter heisst es in 6.7:
The zero-initialization (_dcl.init_) of all local objects with static
storage duration (_basic.stc.static_) is performed before any other
initialization takes place. A local object of POD type
(_basic.types_) with static storage duration initialized with con-
stant-expressions is initialized before its block is first entered. 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 (_basic.start.init_).
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. If the initialization exits by
throwing an exception, the initialization is not complete, so it will
be tried again the next time control enters the declaration. If con-
trol re-enters the declaration (recursively) while the object is being
initialized, the behavior is undefined.mfg
v R