Topic: initialization of namespace members


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 22 Dec 1994 06:42:28 GMT
Raw View
smeyers@netcom.com (Scott Meyers) writes:

>When are members of namespaces initialized?  For example,

>  namespace XXX {
>    class Foo { ... };  // assume Foo has a user-defined default ctor
>    Foo thisIsAFoo;
>    ...
>  };
>
>Is XXX::thisIsAFoo initialized during static initialization?

Yes.

>Does it matter if thisIsAFoo is never referenced?

No. Construction and destruction of a named variable may never be
optimized away if the constructor or destructor has side effects.
If neither has side effects and you never reference the variable,
you can't tell (staying within the language) whether it was
initialized, so you shouldn't care.

--
Steve Clamage, stephen.clamage@eng.sun.com




Author: esap@kaarne.cs.tut.fi (Pulkkinen Esa)
Date: 22 Dec 1994 17:27:27 GMT
Raw View
Scott Meyers (smeyers@netcom.com) wrote:
: When are members of namespaces initialized?  For example,

:   namespace XXX {
:     class Foo { ... };  // assume Foo has a user-defined default ctor
:     Foo thisIsAFoo;
:     ...
:   };
:
: Is XXX::thisIsAFoo initialized during static initialization?  Does it
: matter if thisIsAFoo is never referenced?  For example, if the namespace
: above is followed by

I believe static initialization is the only possibility here. If the compiler
were required to check references to XXX::thisIsAFoo, it would have to
generate code to check for the possible initialization in the beginning of
each function (or worse, on every reference), that uses the namespace.

IMHO for the initialization of the variable(s) in a namespace, you should be
able (I think it's not currently so) to define functions
(constructor/destructor) that take care of the initialization and
destruction of variables in the namespace. My (rather old) version of
the WP doesn't really say much about the initialization of variables
in a namespace.
--
   Esa Pulkkinen                        | Please forgive me if my information
   E-Mail:  esap@cs.tut.fi              | is outdated/wrong. All the opinions
   WWW   :  http://www.cs.tut.fi/~esap/ | are completely mine.







Author: smeyers@netcom.com (Scott Meyers)
Date: Wed, 21 Dec 1994 18:53:44 GMT
Raw View
When are members of namespaces initialized?  For example,

  namespace XXX {
    class Foo { ... };  // assume Foo has a user-defined default ctor
    Foo thisIsAFoo;
    ...
  };

Is XXX::thisIsAFoo initialized during static initialization?  Does it
matter if thisIsAFoo is never referenced?  For example, if the namespace
above is followed by

  main(){}

and that's the entire program, do I know that XXX::thisIsAFoo was
initialized?

Thanks,

Scott