explicit vector(size_type n);
Effects: Constructs a vector with n default constructed elements.
A default constructed element is default-initialized, right? (According to
the definition of default-initialization, [dcl.init].) So a default
constructed int is zero, right?
While a default constructed aggregate struct may not be entirely initialized:
struct Foo { int i; std::string s; };
For a default constructed Foo, its int data member will have an indeterminate
value, right?
Somehow I thought that the Standard would state that those STL container
elements would be value-initialized, but apparently I was wrong...
Kind regards,
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
No, it's the other way around: a default-initialized non-POD class
object is default-constructed - but a default-constructed object is
not one that was necessarily default-initialized. In particular,
objects created inside a Standard Library container are default-
constructed - but they are but not default- (or value- or zero-)
initialized.
> While a default constructed aggregate struct may not be entirely initialized:
>
> struct Foo { int i; std::string s; };
>
> For a default constructed Foo, its int data member will have an indeterminate
> value, right?
Yes, the "i" data member will have an indeterminate initial valae for
a default-constructed Foo object (such as one created inside a
std::vector). The "i" data in a default-initialized Foo object (such
as one in a constructor-initializer list) - on the other hand - will
have an initial value of zero. It's important to remember here that a
default-initialized POD class object - is not default-constructed.
> Somehow I thought that the Standard would state that those STL container
> elements would be value-initialized, but apparently I was wrong...
No, only declared objects can be value-, default- or zero-initialized.
In other words, these kinds of object initialization require a
specific "syntactical context." In this case, the user program does
not declare the objects that a std::vector's constructor creates
inside the vector (nor does the std::vector implementation itself have
any obligation to declare them itself), In fact, a std::vector
constructor simply default-constructs the elements that need to be
placed in the container - and does so without initializing those
elements in any of the ways described in.§8.5 (Initializers) of the C+
+ Standard.
Greg
--
Greg Herlihy wrote:
> No, it's the other way around: a default-initialized non-POD class
> object is default-constructed - but a default-constructed object is
> not one that was necessarily default-initialized.
Thank you, Greg. So I confused the terms "default constructed" and
"default-initialized"! Do you also know the value of a "default constructed"
int (or any scalar type)? Is it zero, or indeterminate? I couldn't find it
in the Draft.
>> struct Foo { int i; std::string s; };
>>
>> For a default constructed Foo, its int data member will have an
>> indeterminate value, right?
>
> Yes, the "i" data member will have an indeterminate initial value for
> a default-constructed Foo object (such as one created inside a
> std::vector). The "i" data in a default-initialized Foo object (such
> as one in a constructor-initializer list) - on the other hand - will
> have an initial value of zero.
I think you're talking about a value-initialized Foo object here...
>> Somehow I thought that the Standard would state that those STL
>> container elements would be value-initialized, but apparently I
>> was wrong...
> No, only declared objects can be value-, default- or zero-initialized.
The expression "new Foo()" creates a value-initialized object. So is there a
declared object in that case?
> In other words, these kinds of object initialization require a
> specific "syntactical context." In this case, the user program does
> not declare the objects that a std::vector's constructor creates
> inside the vector (nor does the std::vector implementation itself have
> any obligation to declare them itself), In fact, a std::vector
> constructor simply default-constructs the elements that need to be
> placed in the container - and does so without initializing those
> elements in any of the ways described in.§8.5 (Initializers) of the
> C++ Standard.
Thanks so far...!
Kind regards,
Niels
I would say that the relevant comparison is really between objects
declared with initializers and those declared without. As it turns
out, non-POD class objects without initializers are default
initialized anyway, so the absence of an initializer when declaring
that type of object usually does not matter.
For other types, such as a POD type, a missing initializer in a
declaration can make a big difference: For example:
int i; // no initializer present - value of i is indeterminate
int i(); // i is value-initialized - value of i is zero
{ int i(); declares a function named i. -mod/sk }
> The expression "new Foo()" creates a value-initialized object. So is there a
> declared object in that case?
You are right, objects can also be value-initialized in a new
expression (with a "new initializer" syntax) - so an object can be
initialized in C++ without being declared.
Greg