R
Ron Natalie
Ok, here's the poop on initialization.Dave said:Having followed the whole thread, I'm still a bit confused. I wonder if
someone might post a concise but comprehensive summary of behavior under
both C++98 and C++2003 as well as definitions of relevant terms such as
default-initialize, value-initialize, zero-initialize, etc...
This part is the same before and after TC1.
There is a concept called default initialization. Default initialization means for, non-POD types
to run the constructor. For POD types, it means zero initialization. However, the confusing issue
is that POD's are sometimes not default initialized. For example, default initialization is skipped
with allocation via new (without the parens) and also for auto (local non-static) variables. This
I believe is a major defect in the language, but you'll never convince the bean counters that
consistant behavior is worth making people clean up their old code if they really want to create
an uninitialized variable.
What TC1 added is value initialization. Value initialization was added to the new() case and
what it says is that if the non-POD class has no default constructor, then any pod's would be
zero initialized where they wouldn't have been in the default initialization case.
The item it is fixing is this:
struct X {
SomeType st;
PodType pod;
};
new X(); in pre-TC1, if SomeType was not POD, then X would not be POD, and hence pod
would not be initialized (because X's implicitly generated constructor doesn't do it). In post TC1,
the value initialization is applied to st and pod when X is value initialized. st's implicit constructor
runs and pod gets zero'd.