static local object initialization

A

AlexWalk

Hello. The C++ textbooks or articles I've read so far state that
static local objects are initialized the first time the control flows
over their declarations. For example, "The C++ Programming Language"
by Bjarne Stroustrup says so. The C++ FAQ Lite says so. But today I
accidentally read about the ISO C++ standard, which doesn't says so.
It says:

[6.7.4] 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
(3.6.2). Otherwise such an object is initialized the first time
control passes through its declaration; ...

Does this mean the statements in those C++ textbooks are incorrect?
Does this mean depending on the "intialization-on-first-entry"
behavior will have some unexpected result?
 
A

Alf P. Steinbach /Usenet

* AlexWalk, on 23.06.2010 06:00:
Hello. The C++ textbooks or articles I've read so far state that
static local objects are initialized the first time the control flows
over their declarations. For example, "The C++ Programming Language"
by Bjarne Stroustrup says so. The C++ FAQ Lite says so. But today I
accidentally read about the ISO C++ standard, which doesn't says so.
It says:

[6.7.4] 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
(3.6.2). Otherwise such an object is initialized the first time
control passes through its declaration; ...

Does this mean the statements in those C++ textbooks are incorrect?

No, it's just an "as if" optimization, hence the reference to conditions.

Does this mean depending on the "intialization-on-first-entry"
behavior will have some unexpected result?

No.


Cheers & hth.,

- Alf
 
A

AlexWalk

* AlexWalk, on 23.06.2010 06:00:




Hello. The C++ textbooks or articles I've read so far state that
static local objects are initialized the first time the control flows
over their declarations. For example, "The C++ Programming Language"
by Bjarne Stroustrup says so. The C++ FAQ Lite says so. But today I
accidentally read about the ISO C++ standard, which doesn't says so.
It says:
[6.7.4] 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
(3.6.2). Otherwise such an object is initialized the first time
control passes through its declaration; ...
Does this mean the statements in those C++ textbooks are incorrect?

No, it's just an "as if" optimization, hence the reference to conditions.

Oh, I see the difference. The standard says "statically initialize".
Thanks for your reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top