Is Vector::resize() Behavior Consistent Under C 11 and Boost.Container?
The behavior of std::vector::resize() is commonly used for temporarily buffering data, resizing them to the appropriate size before use. In C 03, std::vector::resize() created new elements by copying, but C 11 introduced an overload that initializes new elements without copying.
Boost.Container provides an additional default_init overload for boost::container::vector::resize(), which initializes new elements with default values. This feature aligns with C 11's design philosophy of only paying for what you need.
Upon testing the behavior of std::vector and boost::container::vector under both C 03 and C 11 modes, unexpected results were observed. In both C 03 and C 11, resizing the vector without specifying initialization still resulted in zero initialization of new elements for both std::vector and boost::container::vector.
This behavior indicates that the interface change in std::vector has no effect on the implementation, and the final elements added in resize() are still initialized with zeros. The issue raised is whether this behavior is correct.
The above is the detailed content of Does `std::vector::resize()` and `boost::container::vector::resize()` Exhibit Consistent Zero-Initialization Behavior Across C Standards?. For more information, please follow other related articles on the PHP Chinese website!