Topic: stl and stl default constructors


Author: gp1@paradise.net.nz (Graeme Prentice)
Date: Wed, 21 May 2003 19:28:08 +0000 (UTC)
Raw View
On 16 May 2003 21:50:07 GMT, amelie.francois@free.fr (Francois Dumont)
wrote:

>Hello
>
>I need a confirmation. According the standard should:
>
>std::vector<int> intVector(10);
>
>be a vector full of 0?
>
>I already check some topics about basic types default constructor but
>haven't found anything about their behavior inside the STL.

The C++ standard doesn't define the meaning of default construction for
built in types  - there is no such thing.  Respected authors such as
Stroustrup & Josuttis state that the expression T()  for a built-in
type T means default construction and zero-initialisation but this is
not backed up by the C++ standard which defines this to be default
initialisation not default construction.  TC1 changes T() to mean value
initialisation.

In section 23.2.4 the C++ standard defines this constructor for vector

explicit vector(size_type n, const T& value = T(),
const Allocator& = Allocator());

The second parameter has a default argument of T().

Section 5.2.3 in the standard states
<quote>
2 The expression T(), where T is a simpletypespecifier (7.1.5.2) for a
nonarray complete object type or the (possibly cvqualified)
void type, creates an rvalue of the specified type, whose value is
determined by default initialization.
<end quote>

A defect report (and TC1) change "default initialization" to be "value
initialization" in 5.2.3 para 2 for T().  The 1998 standard left an
unintentional "hole" for the construction T()  - default initialization
produces zero-initialization for built in types (the case you're
interested in) and for non POD class types, the default constructor is
called.  The "hole" is with non POD class types that have no constructor
- for these types, the default constructor leaves POD members of the
class uninitialised - this was not intended.

e.g.
class x1 {
 int a;
 std::string s1;
};

std::vector<x1> v1(10);

produces a lot of uninitialised ints and default constructed strings.

TC1 changes the definition of T() (whereever it's used - new
expressions, mem-initialisers etc) to be value initialisation  - here is
the definition from the defect report of value initialisation

<quote)
To value-initialize an object of type T means:

if T is a class type (clause 9  class) with a user-declared constructor
(12.1  class.ctor), then the default constructor for T is called (and
the initialization is ill-formed if T has no accessible default
constructor);
if T is a non-union class type without a user-declared constructor, then
every non-static data member and base-class component of T is
value-initialized;
if T is an array type, then each element is value-initialized;
otherwise, the object is zero-initialized.
<end quote>

This means that the example I gave above for class x1, will have it's
POD members zero-initialised in the vector.  I know of one compiler that
doesn't zero-initialize POD structs for the construction T() so as
always, you need to test your code works correctly with whatever
compilers you use.

Graeme

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: amelie.francois@free.fr (Francois Dumont)
Date: 16 May 2003 21:50:07 GMT
Raw View
Hello

I need a confirmation. According the standard should:

std::vector<int> intVector(10);

be a vector full of 0?

I already check some topics about basic types default constructor but
haven't found anything about their behavior inside the STL.

Thanks

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Edward Diener" <eldiener@earthlink.net>
Date: 16 May 2003 22:25:05 GMT
Raw View
Francois Dumont wrote:
> Hello
>
> I need a confirmation. According the standard should:
>
> std::vector<int> intVector(10);
>
> be a vector full of 0?
>
> I already check some topics about basic types default constructor but
> haven't found anything about their behavior inside the STL.

The values have the default value of the type, which is in this case 'int',
and the default value for 'int' is 0, ie. int i(); sets i to 0.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: m.collett@auckland.ac.nz (Matthew Collett)
Date: Mon, 19 May 2003 17:38:36 +0000 (UTC)
Raw View
In article <Ucdxa.1255$Io.100173@newsread2.prod.itd.earthlink.net>,
 "Edward Diener" <eldiener@earthlink.net> wrote:

> The values have the default value of the type, which is in this case 'int',
> and the default value for 'int' is 0, ie. int i(); sets i to 0.

ITYM "declares i to be a function taking no arguments and returning an
int". ;-)

(This doesn't affect the answer to the OP's question, which is still
'Yes'.)

Best wishes,
Matthew Collett

--
Those who assert that the mathematical sciences have nothing to say
about the good or the beautiful are mistaken.          -- Aristotle

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]