Re: initializing static class variables

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 08/01/04


Date: Sun, 01 Aug 2004 07:36:46 +0100

On Sat, 31 Jul 2004 23:28:10 -0400, Mark <mark_netNews@hotmail.com> wrote:

> On Sat, 31 Jul 2004 20:12:43 +0100, "John Harrison"
> <john_andronicus@hotmail.com> wrote:
>
>
>> I believe it was introduced in 2003 standard but I can't be sure since I
>> don't have the 1998 standard available.
>>
>> I guess the difference is best illustrated with an example
>>
>> struct Foo
>> {
>> int x;
>> };
>>
>> The standard says (8.5 para 5) that if Foo is default initialised then
>> its
>> default constructor is called. This is obviously an implicit default
>> constructor and 12.1 para 7 and 12.6.2 para 4 together say that x is not
>> initialised in this case (i.e. it has an undefined value).
>>
>> Hoever 8.5 para 5 says that if Foo is value initialised then because Foo
>> does not have a user-declared constructor each of its members is value
>> initialised. And when a scalar type is value initalsed it is in fact
>> zero
>> initialised.
>>
> [....]
>>
>> But I'm still learning this stuff myself, so watch out I could easily be
>> wrong.
>>
>> john
>
> Default initilization when viewed from the 1998 and 03 version of the
> standard seem contradictory.
>
> The 1998 standard for C++ (ISO/IEC 14882-1998) says:
>
> "An object whose initializer is an empty set of parentheses, i.e., (),
> shall be default-initialized."

That's directly contradicted by the 2003 standard which says value
initialised.

>
> A little before that, it says:
>
> "To default-initialize an object of type T means:
> ** if T is a non-POD class type (clause 9), the default constructor
> for T is called (and the initialization is ill-formed if T has no
> accessible default constructor);
> ** if T is an array type, each element is default-initialized;
> ** otherwise, the storage for the object is zero-initialized."
>
> (Section 8.5, Initializers)
>
> So given
>
> struct data_t
> {
> int Idx;
> int Jdx;
> int Kdx;
> } data;
>
> data_t type is a POD class type (plain old data), so with empty
> parentheses, it should be zero-initialized.

Assuming you are talking about the 1998 standard I don't think so. Look up
the definition of compiler generated default constructor. I think you will
see that it says Idx, Jdx and Kdx remain uninitialised not zero
initialised.

>
> data_t() is not exactly a constructor call. It denotes a temporary
> object of type data_t, constructed with default initialization.
>
> So data(data_t()) initializes the object named "data" with a
> temporary, default-initialized object of its type, and for this type
> default initialization is zero-initialization.
>
> Note that some compilers may not do this right. In any event, it
> seems to me the 03 version of the standard refers to this as value
> initilization.

And that's the crucial difference, because value initialisation does not
use the compiler generated default constructor.

john



Relevant Pages

  • Re: inhibit compiler warning C4624 for a class hierarchy
    ... The undefined behavior means that the behavior is decided by the compiler. ... could appear due to member destructors not being called, ... I guess I can make the two-step initialization ... Compiler behavior would be well defined by the standard. ...
    (microsoft.public.vc.language)
  • Re: copy constructor question
    ... > Kurt Krueckeberg wrote: ... >> Or, is the code generated for x2more properly equivalent, according to the standard, to something like ... >> x2 results in just a single call to the copy constructor. ... compiler may perform do not constitute a valid prediction ...
    (comp.lang.cpp)
  • Re: initializing static class variables
    ... >don't have the 1998 standard available. ... >does not have a user-declared constructor each of its members is value ... "To default-initialize an object of type T means: ... object of type data_t, constructed with default initialization. ...
    (comp.lang.cpp)
  • Re: Component Initialization of Module Variables
    ... who does not worry about how the compiler would go about ... from the standard and from textbooks, leaving just the error message as ... has "component initialization", which isn't a term used in the standard, ...
    (comp.lang.fortran)
  • Re: Memory leak in derived type pointer intialization
    ... > that variable has some components that have default initialization. ... but the standard doesn't specify just when the ... > it can pick up via explicit initialization or the SAVE statement ... I have inserted a debugging message to print out if the pointer ...
    (comp.lang.fortran)