Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2005-08-19 10:48:07


From: "Robert Ramey" <ramey_at_[hidden]>
> Vladimir Prus wrote:

> In this case:
>
> void f(){
> static X x;
> ...
> }
>
> The constructor for x is invoked the first time f() is called. In this

Yes.

> case:
>
> class X {
> static X x;
> ...
> };
>
> X X::x.
>
> The constructor X::x is invoked at pre-execution time. At least that's my
> understanding and it seems that all the compiler's in our test suite seem to
> support this behavior.

X::x's static initialization will occur before main(). If X::x
requires dynamic intialization, it must occur before the first
function following it's definition in that translation unit is
invoked.

Nothing more is guarranteed.

IOW, if the constructor of X requires runtime behavior--opens a
file, allocates memory, calculates a number, etc.--then that
isn't guarranteed to happen until some function, defined after
X::x in the same translation unit, is invoked.

> The implementation of
> BOOST_CLASS_EXPORT depends on static constructors being executed at
> pre-execution time or for the case of DLLS - up DLL loading

By "pre-execution time," I assume you mean before main() is
called. That only applies to static initialization, not dynamic
initialization of statics.

> > Now, on Unix all static initialisers are run when the program is first
> > loaded, so I probably should not care if some other users run into
> > problems.
>
> I don't believe this is a feature of the operating system but rather the
> compiler. I also believe that the serialization library presumes correctly
> that they will be invoked a pre-execution time. I never really thought of

Your assumption is ill founded.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk