Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!zaphod.mps.ohio-state.edu!wuarchive!uunet!zephyr.ens.tek.com!uw-beaver!milton!sumax!polari!rwing!seaeast!sunbrk!Usenet From: William.M.Miller@sunbrk.FidoNet.Org (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: Initializing Global Objects Message-ID: <675518012.3@sunbrk.FidoNet> Date: 27 May 91 23:50:26 GMT Sender: Usenet@sunbrk.FidoNet.Org Lines: 66 sdm@cs.brown.edu (Scott Meyers) writes: > We know that nifty_counter::count is preinitialized to 0 because it is > static, but the rules of the language also require that it be initialized > by the programmer somewhere. Actually, that's not quite accurate; what is required is that it be *defined* somewhere; the initialization is optional. > My question is this: to what value should it > be initialized? > > In the iostream library, the value used is 0, i.e., one of the translation > units contains the statement: > > int nifty_counter::count = 0; > > Prior to this statement is the declaration of the static nifty_counter > object, so we know that the nifty_counter constructor will have been called > by the time that nifty_counter::count is explicitly initialized. As a > result, at the time when nifty_counter::count is initialized, it's value > will no longer be 0, since it will have been incremented inside the > constructor. If its value is reset to 0 (the explicit initialization > value), then if a different nifty_counter object is subsequently > initialized (possible, since it could be from a different translation > unit), won't X be initialized *again*? > > In other words, shouldn't the proper initialization value of count be as > follows? > > int nifty_counter::count = nifty_counter::count; Well, that would work, but it's redundant; the best definition is: int nifty_counter::count; // implicitly initialized to 0 As you point out, this default initialization to 0 occurs before any of the runtime static initializations, normally at load time, so the serialization is correctly maintained. However, the question you bring up is an interesting one. The order of static initializations in different compilation units, except for the default initialization to 0, is undefined. Although most (all?) current implementations treat int nifty_counter::count = 0; as completely synonymous with the default initialization, i.e., done at load time, there's nothing I can find in the current specification that requires that; in other words, it would be perfectly legal for an implementation to run the nifty_counter constructors in few compilation units, incrementing nifty_counter::count from its default initialization value of 0, and then run the *explicit* initialization of nifty_counter::count, resetting the value to 0. I've relayed this question to the X3J16 ANSI Standard C++ Committee for discussion; I think it's possible that the current specification is due to an oversight and that initializations like this are intended to be done prior to runtime static initializations. Until this question is resolved, however, the safest thing to do is to write static member definitions without initializers and rely on the implicit initialization to 0. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com * Origin: Seaeast - Fidonet<->Usenet Gateway - sunbrk (1:343/15.0)