Static data initialised when my program EXITS

Sometimes I like to put initialisation code in the constructor of a static object, like this.

namespace
{
    struct Init {
        Init ();
    };

    Init :: Init () {
        // whatever
    }

    Init init;
}

Yes, I know this is bad in general because you can't rely on the order of initialisations between translation units, but sometimes, for some things, it's okay.

Anyway I don't like the syntax and today I tried out this.

namespace
{
    int init = [] () -> int {
        // whatever
        return 0;
    } ();
}

When the program ran, the whatever code ran after main()!

I know static initialisations like this happen in an arbitrary order, but I thought the specification required it all, in whatever order, to happen before main() enters.

Is the compiler misbehaving or is there further subtlety?

#> gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.8/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-i386/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-i386 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-i386 --with-arch-directory=i386 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-targets=all --enable-multiarch --disable-werror --with-arch-32=i686 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9) 

Answers


Found this in the standard (3.6.2 Initialization of non-local variables [basic.start.init], 4):

It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized.* * A non-local variable with static storage duration having initialization with side-effects must be initialized even if it is not odr-used (3.2, 3.7.1).)

Seems to say explicitly that in at least some cases, initialization can be deferred until after main enters.


Need Your Help

What is the best code location to define table relationship methods using Zend Framework models?

php database model-view-controller zend-framework orm

I'm creating a simple tag system that allows me to link a tag to almost everything in my application. In order to facilitate this, I created a table called "objects", referenced by my model "Object...

Query on support for profiling native unmanaged C++ code in Visual Studio 2012 & 2013

visual-studio visual-studio-2012 profiling visual-studio-2013 instrumentation

I've installed Visual Studio 2012 & 2013 Professional for evaluation with all the latest updates with a view to purchasing a licence if all goes well...