Order of class static members initialization

Discussion in 'C++' started by BigMan, Feb 24, 2005.

  1. BigMan

    BigMan Guest

    Does the standard define the order in which static members of a class
    are created and initialized?
     
    BigMan, Feb 24, 2005
    #1
    1. Advertising

  2. BigMan wrote:
    > 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
     
    Victor Bazarov, Feb 24, 2005
    #2
    1. Advertising

  3. BigMan

    BigMan Guest

    Could you tell where in the standard this is stated?
     
    BigMan, Feb 24, 2005
    #3
  4. BigMan

    Ron Natalie Guest

    BigMan wrote:
    > 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).
     
    Ron Natalie, Feb 25, 2005
    #4
  5. BigMan

    Axter Guest

    >> 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.



    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!
     
    Axter, Feb 25, 2005
    #5
  6. "Axter" <> wrote...
    >>> 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.

    >
    >
    > 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?

    > [..]
     
    Victor Bazarov, Feb 26, 2005
    #6
  7. BigMan

    Ron Natalie Guest

    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.
     
    Ron Natalie, Feb 26, 2005
    #7
  8. BigMan

    Axter Guest

    >>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?


    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.
     
    Axter, Mar 1, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,525
    Tony Morris
    Oct 19, 2004
  2. JFCM
    Replies:
    4
    Views:
    5,788
  3. DanielBradley
    Replies:
    3
    Views:
    14,991
    Dave Moore
    Jun 2, 2004
  4. Dennis Jones
    Replies:
    2
    Views:
    345
    Dennis Jones
    Jan 5, 2007
  5. Victor Bazarov
    Replies:
    11
    Views:
    773
    James Kanze
    Dec 9, 2009
Loading...

Share This Page