Topic: order of initialization of statics


Author: Greg Janee <gjanee@sb.grci.com>
Date: 1997/06/26
Raw View
I'm having trouble understanding exactly what the standard guarantees
with respect to the order of initialization of (non-local) static
variables.

Section 3.6.2 (page 3-23 of the 12/2/96 draft) gives the following
example:

    // -- File 1 --
    #include "a.h"
    #include "b.h"
    B b;
    A::A () { b.Use(); }

    // -- File 2 --
    #include "a.h"
    A a;

    // -- File 3 --
    #include "a.h"
    #include "b.h"
    extern A a;
    extern B b;
    main () { a.Use(); b.Use(); }

and says, "...if a is defined before main is entered, it is not
guaranteed that b will be initialized before it is used by the
initialization of a, that is, before A::A is called."  But the previous
paragraph says, "it is implementation-defined whether the dynamic
initialization of an object ... is done before the first statement of
main or ... any point in time .. before the first use of a function or
object in the same translation unit."  Now, A::A is in the same
translation unit as b, so doesn't that imply that b must be initialized
before A::A is called??

Desperately seeking simplicity,
-Greg

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Janee                                     GRC International, Inc.
gjanee@sb.grci.com                                 5383 Hollister Ave.
                                          Santa Barbara, CA 93111-2349
                               805-964-7724 (voice) 805-967-7094 (fax)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]