Re: Vector et al default initialized
JoshuaMaurice@gmail.com ha scritto:
Taken from the standard
23.2.4.1 vector constructors, copy, and assignment [lib.vector.cons]
...
explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());
Is there a specific reason they did not instead choose the following
(or something similar) instead?
explicit vector(size_type n);
explicit vector(size_type n, const T& value, const Allocator& =
Allocator());
explicit vector(size_type n, const Allocator& );
Specifically, I like to view std::vector as a better (built in) array.
However, because of this interface (and the semantics implied by it,
and formally spelled out in the standard), vectors incur an
unnecessary performance cost during construction. (A similar situation
probably exist for destruction.) A local array
int some_array[10];
does not initialize its elements. The equivalent local vector
std::vector<int> some_vector(10);
does initialize its elements. The signature of the function takes a
default argument for "value" of the default constructor for the
contained type, and then copy constructs each contained element from
the object returned by the default constructor.
This seems like a curious design decision when a main design goal of C+
+ is to be as fast as possible (while weighed against its other design
goals, like usability and platform independence).
The problem here is that vector is allowed to copy elements on certain
occasions (for example when reallocation occurs) and so you must ensure
that all elements in the vector are in "copyable" state. There are,
unfortunately, exotic architectures that have "trap representations" of
objects that cannot be copied. The typical example is with pointers:
there are computers that actually check if a pointer is valid even
during a copy operation and not only when it's dereferenced (this is not
done explicitly in the code, but by storing pointers in specific CPU
registers). So in order to guarantee that every object is "copyable" the
only way is to initialize everything.
Note that if the contained type is a class type, in the array case,
each element will be default constructed, and in the vector case, each
element will be copy constructed from a default construct object. It
differs in the case of PODs, where "default constructed" means
uninitialized, so the array is unitialized, and the vector is
initialized.
This is a common misconception... What you said is wrong. According to
8.5/8: "An object whose initializer is an empty set of parentheses,
i.e., (), shall be value-initialized." Value-initialization for arrays
of built-in types means that the array shall be zero-initialized (see
8.5/5). However, one reason behind this misconception is that the are a
few compilers that do not implement this rule correctly. That is a bug.
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]