Order of class static members initialization

B

BigMan

Does the standard define the order in which static members of a class
are created and initialized?
 
V

Victor Bazarov

BigMan said:
Does the standard define the order in which static members of a class
are created and initialized?

Static members of the class are initialised in the order in which they
are defined. Just like any other namespace object with static storage
duration.

If they are defined in different translation units, the relative
initialisation order is unspecified.

V
 
R

Ron Natalie

BigMan said:
Could you tell where in the standard this is stated?

Described in 3.6.2. The rules for statics at namespace scope is
there (9.4.2 covers the few special constant intializing of
static data members that doesn't occur at namespace scope and
further amplifies that 3.6.2 applies to the others).
 
A

Axter

Does the standard define the order in which static members of a
Static members of the class are initialised in the order in which they
are defined. Just like any other namespace object with static storage
duration.
If they are defined in different translation units, the relative
initialisation order is unspecified.


Actually, it's not that simple.

POD objects will get initialize before non-POD non-constant object.
Even that is not entirely accurate.

The order of initialization depends on some of the following factors
Is object initialize to zero
Is object constant
Is object POD
Order of definition

And having multiple static objects in different translation units
throws a monkey wrench in the whole thing!
 
V

Victor Bazarov

Axter said:
Actually, it's not that simple.

POD objects will get initialize before non-POD non-constant object.
Even that is not entirely accurate.

Perhaps you care to actually provide an accurate description...
The order of initialization depends on some of the following factors
Is object initialize to zero
Is object constant
Is object POD
Order of definition

Do you have any proof of this? How is initialising with 0 versus with
something else affects the order? Imagine your translation unit has
this single declaration statement:

int a = 0, b = 42, c = 0;

what is the order of things initialised?
 
R

Ron Natalie

Victor Bazarov wrote:
=
Do you have any proof of this? How is initialising with 0 versus with
something else affects the order? Imagine your translation unit has
this single declaration statement:

int a = 0, b = 42, c = 0;

what is the order of things initialised?

The thread is statics (non-local objects) Declaration statemetns
aren't germain.

He garbled his description to the point of being wrong. Whether something
is constant or not is IMMATERIAL to the order of initialization. Things
that are initialized with constants do make a difference.

Those statics that are to be zero initialized happen before anything else.
POD's initialized with constant expressions come next.
Then all your dynamic initialization takes place.

This is handy as it allows you to conditionalize the functions in the dynamic
initialization by using a static POD with a constant (or zero)initializer.
 
A

Axter

Perhaps you care to actually provide an accurate description...
Yes, that was poorly worded on my part.
That should have been Zero-Initialized, and not initialized to zero.
Read section 3.6.2 of the C++ standard.
**************************************************************************
Zero-initialization and initialization with a constant expression are
collectively called static initialization; all other innitialization
is dynamic initialization. Objects of POD types with static storage
duration initialized withconstant expressions shall be initialized
before any dynamic initialization takes place. Objects with static
storage duration defined in namespace scope in same translation unit
and dynamically initialized shall be initialized in the order in which
their definition appears in the translation unit.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top