Topic: creating a container with n elements
Author: mark.vandijk@perform-sol.com (Mark van Dijk)
Date: Tue, 21 Oct 2003 18:45:11 +0000 (UTC) Raw View
Hi there, consider the following line of code:
std::vector<X> vec(1000);
It is my understanding that all 1000 elements of this vector are created
by the default constructor of X. This understanding is based on "The
Standard C++ Library" by Josuttis page 232.
But my compiler (MS Visual Studio .NET 2003), appears to create the
first element with the default constructor, but the remaining 999
elements are created by copy-constructing the first element.
Have i completely missed something really obvious? Is this correct
behaviour according to the standard?
---
[ 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: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 21 Oct 2003 19:20:50 +0000 (UTC) Raw View
Mark van Dijk wrote:
> std::vector<X> vec(1000);
>
> It is my understanding that all 1000 elements of this vector are created
> by the default constructor of X.
No. See 23.2.4.1. The vector constructor you are calling has a second
argument which is used as the initial value for the elements. If you
leave it out, it defaults to a default-constructed X, which is the single
call to the default constructor that you see. The vector elements are
copy constructed from this object.
---
[ 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: kuyper@wizard.net (James Kuyper)
Date: Tue, 21 Oct 2003 22:32:44 +0000 (UTC) Raw View
mark.vandijk@perform-sol.com (Mark van Dijk) wrote in message news:<3f94def6$1@news.iconz.co.nz>...
> Hi there, consider the following line of code:
>
> std::vector<X> vec(1000);
>
> It is my understanding that all 1000 elements of this vector are created
> by the default constructor of X. This understanding is based on "The
> Standard C++ Library" by Josuttis page 232.
>
> But my compiler (MS Visual Studio .NET 2003), appears to create the
> first element with the default constructor, but the remaining 999
> elements are created by copy-constructing the first element.
> Have i completely missed something really obvious? Is this correct
> behaviour according to the standard?
The standard requires that X be a CopyConstructible type(23.1p3),
which means that an object created by copy construction is equivalent
to the original, and can therefore be substituted for it wherever
desired. Therefore, this is a legitimate implementation of that
function, and is an example of precisely why the CopyConstructible
requirement exists.
---
[ 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: dhruvbird@gmx.net ("Dhruv")
Date: Wed, 22 Oct 2003 15:51:08 +0000 (UTC) Raw View
On Tue, 21 Oct 2003 18:45:11 +0000, Mark van Dijk wrote:
> Hi there, consider the following line of code:
>
> std::vector<X> vec(1000);
>
> It is my understanding that all 1000 elements of this vector are created
> by the default constructor of X. This understanding is based on "The
> Standard C++ Library" by Josuttis page 232.
>
> But my compiler (MS Visual Studio .NET 2003), appears to create the
> first element with the default constructor, but the remaining 999
> elements are created by copy-constructing the first element.
>
> Have i completely missed something really obvious? Is this correct
> behaviour according to the standard?
Yes, it is. But most probably, your implementation is default constructing
1 object, and copy constructing all the 1000 objects from that 1 object.
This version of the ctor takes 2 arguments instead of the 1 that you have
mentioned. The 2nd one is a default argument that has the default ctor
invoked T(), and the copies are created form that object.
Regards,
-Dhruv.
---
[ 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 ]