Topic: stl and basic types default constructors
Author: amelie.francois@free.fr (Francois Dumont)
Date: Sun, 18 May 2003 02:33:50 +0000 (UTC) Raw View
> >
> > 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.
>
Thanks for the answer but it made me thought about an other point. Do
pointers have default value?
Should
std::vector<int*> intPtrVector(10);
be full of null pointers?
---
[ 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: jdennett@acm.org (James Dennett)
Date: Sun, 18 May 2003 22:01:20 +0000 (UTC) Raw View
Francois Dumont wrote:
>>>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.
>>
>
>
> Thanks for the answer but it made me thought about an other point. Do
> pointers have default value?
>
> Should
>
> std::vector<int*> intPtrVector(10);
>
> be full of null pointers?
Yes, but note that this question would be more appropriate
for comp.lang.c++.moderated. Followups set accordingly.
The declaration of intPtrVector above is roughly equivalent to
typedef int* ptr_to_int;
std::vector<int*> intPtrVector(10, ptr_to_int());
and ptr_to_int() gives a null pointer. As a technicality,
the elements of the container are copy constructed from the
one default constructed argument (whether explicitly
supplied or from the default argument), rather than being
default constructed themselves.
-- James.
---
[ 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: modeleromy-remove-no-spam@yahoo.com ("Romulus Marius Mare")
Date: Mon, 19 May 2003 17:38:44 +0000 (UTC) Raw View
>
> std::vector<int*> intPtrVector(10);
>
> be full of null pointers?
I am not sure but the standard in 8.5 paragraph 5 says:
To zero-initialize storage for an object of type T means:
- if T is scalar type (3.9), the storage is set to the value of 0 (zero)
converted to T.
In 3.9 I can see that int* is a scalar type.
As I understand the standard the answer to your question IMHO should be yes.
I should do some tests with some compilers to really check this.
Romulus
---
[ 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 ]