James Kanze
12/19/2008 8:10:00 PM
On Dec 19, 12:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> James Kanze wrote:
> > On Dec 18, 6:41 pm, "Hicham Mouline" <hic...@mouline.org> wrote:
> >> void C::f() {
> >> static const int keys[] = { 0, ...., 15 };
> >> }
> > No. In both cases, you have static initialization, which is
> > guaranteed to occur before anything else.
> I thought that a static inside a function is initialized the
> first time the function is called?
The standard explicitly says otherwise: "Objects of POD types
with static storage duration initialized with constant
expressions shall be initialized before any dynamic
initialization takes place." (§3.6.2/1).
In fact, in this case, the word static means what it says; such
variables are initialized statically, not dynamically.
> The above function might not be problematic with regard to
> thread safety, but what about something like this:
> void foo()
> {
> static SomeClass object;
> ...
> }
It depends on SomeClass, obviously. Does SomeClass use static
or dynamic initialization?
> Suppose that 'foo()' may be called from more than one thread.
> Can it happen that 'object' is constructed more than once?
Probably. I don't know what the next version of the standard
will say about it, but in current implementations, it's
undefined behavior to enter such a function from several
different threads without some sort of external synchronization.
But what does this have to do with static initialization?
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34