In C 03, std::vector
Using Boost.Container's vector
However, in C 11 mode, both std::vector and Boost.Container's vector still initialized new elements with zeros when using the overload intended for value initialization. This raises the question:
Is the C 11 std::vector::resize() interface change actually effective?
The results suggest that the interface change has not had the intended effect, as new elements are still being initialized in both implementations.
Addendum
To address the limitations of the existing allocator, an alternative allocator adapter has been proposed that provides a safer and more reliable way to achieve value initialization without initializing all types of elements:
template <typename T, typename A=std::allocator<T>> class default_init_allocator : public A { // ... implementation };
This adapter interposes on construct() calls for value-initialization, transforming them into default-initialization calls. It also correctly handles default-initialization.
The above is the detailed content of Does C 11's `std::vector::resize()` Interface Change Effectively Value-Initialize New Elements?. For more information, please follow other related articles on the PHP Chinese website!