c++ - Static data initialised when my program EXITS -


sometimes put initialisation code in constructor of static object, this.

namespace {     struct init {         init ();     };      init :: init () {         // whatever     }      init init; } 

yes, know bad in general because can't rely on order of initialisations between translation units, sometimes, things, it's okay.

anyway don't syntax , today tried out this.

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

when program ran, whatever code ran after main()!

i know static initialisations happen in arbitrary order, thought specification required all, in whatever order, happen before main() enters.

is compiler misbehaving or 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)  

found in standard (3.6.2 initialization of non-local variables [basic.start.init], 4):

it implementation-defined whether dynamic initialization of non-local variable static storage duration done before first statement of main. if initialization deferred point in time after first statement of main, shall occur before first odr-use (3.2) of function or variable defined in same translation unit variable initialized.*
* non-local variable static storage duration having initialization side-effects must initialized if not odr-used (3.2, 3.7.1).)

seems explicitly in @ least cases, initialization can deferred until after main enters.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -