Does C ++ 11 indicate that the vector <int> has zeros in resize ()?
This question concerns the difference in semantics and performance of new [] int and new [] int () and changes from the first to the second wording, possibly unintentionally created by adding the perfect ctor argument forwarding to allocator_traits :: construct (). This question does NOT refer to the rather obvious problem that, by default, ctor runs for all new elements created using the resize () vector.
To me, cleaning elements of vector built-in types when resizing seems like a waste. But VS2012 is implemented in such a way that resize (n) and, as a result, the constructor with the count parameter actually sets the selected array of values ββto 0.
I also found support for this in the standard, ( http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3485.pdf ), but I think it might be a mistake, since he relies on a recent proposal including perfect forwarding:
p. 507:
template <class T, class... Args> static void construct(Alloc& a, T* p, Args&&... args); 5 E ff ects: causes
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...). and since the new int () should set the value to 0 in accordance with section 11 on page 191 of the same document, the correct implementation in the vector is correct.
The question is whether the standard commit really wanted the empty package of parameters to call the construct to cause a change in the default behavior to build the value for the base types.
It is an invariant that the container never contains an uninitialized object. This means that everything that increases the size of the container must initialize all new elements. This is a feature, not a drawback - access to uninitialized undefined values, and you cannot redistribute (or insert) if some of the values ββhave not been initialized. For instance:.
std::vector<int> v; v.resize( 20 ); v.insert( v.begin(), 1 ); The last line will lead to undefined behavior if resize did not initialize the elements it created.