S
Severin Ecker
Hi,
I have a question regarding the initialization order/point in time of
function local objects with static storage duration (C++ standard
sections 6.7.4 and 3.6.2).
Please consider the following code snippet:
x.cpp
void x() {
static Foo foo;
}
y.cpp
void y() {
static Bar bar;
}
main.cpp
// assume proper inclusion of header files
int main() {
x();
y();
return 0;
}
Now, if Foo and Bar are PODs, reading the C++ standard section 6.7.4, an
implemenation is free to zero initialize foo and bar during the static
initialization process before the program flow enters main() and
therefore before either x() or y() are invoked? (and as such, reading
the rules from 3.6.2, the order whether foo or bar will be initialized
first is not specified.) Am I right thus far?
Now consider that Bar and Foo are classes with a non-trivial default
constructors (but constructors that are independent of the other class
as this would probably trigger one of the rules in 3.6.2), what's the
situation there? Will the local variables only be initialized (namely
the constructor be called) when the program flow reaches the local
variable definitions in x() and y() respectively or is the compiler
still allowed to perform early initialization?
Still the order of destruction must be the exact opposite of the order
of construction (even when early initialization is applied and therefore
"ignoring" which function was called first), yes?
One additional question to this: is the order of initialization of
objects with statid storage duration "implementation defined" or
"unspecified" (I guess, relying on the order itself yields undefined
behaviour, correct?)
Many thanks in advance!
best regards,
severin
I have a question regarding the initialization order/point in time of
function local objects with static storage duration (C++ standard
sections 6.7.4 and 3.6.2).
Please consider the following code snippet:
x.cpp
void x() {
static Foo foo;
}
y.cpp
void y() {
static Bar bar;
}
main.cpp
// assume proper inclusion of header files
int main() {
x();
y();
return 0;
}
Now, if Foo and Bar are PODs, reading the C++ standard section 6.7.4, an
implemenation is free to zero initialize foo and bar during the static
initialization process before the program flow enters main() and
therefore before either x() or y() are invoked? (and as such, reading
the rules from 3.6.2, the order whether foo or bar will be initialized
first is not specified.) Am I right thus far?
Now consider that Bar and Foo are classes with a non-trivial default
constructors (but constructors that are independent of the other class
as this would probably trigger one of the rules in 3.6.2), what's the
situation there? Will the local variables only be initialized (namely
the constructor be called) when the program flow reaches the local
variable definitions in x() and y() respectively or is the compiler
still allowed to perform early initialization?
Still the order of destruction must be the exact opposite of the order
of construction (even when early initialization is applied and therefore
"ignoring" which function was called first), yes?
One additional question to this: is the order of initialization of
objects with statid storage duration "implementation defined" or
"unspecified" (I guess, relying on the order itself yields undefined
behaviour, correct?)
Many thanks in advance!
best regards,
severin