Home > Backend Development > C++ > Does C 11's `std::vector::resize()` and Boost.Container's `boost::container::vector::resize()` Always Value-Initialize as Expected?

Does C 11's `std::vector::resize()` and Boost.Container's `boost::container::vector::resize()` Always Value-Initialize as Expected?

DDD
Release: 2024-12-09 08:07:06
Original
688 people have browsed it

Does C  11's `std::vector::resize()` and Boost.Container's `boost::container::vector::resize()` Always Value-Initialize as Expected?

Does Vector::resize() in C 11 and Boost.Container Behave as Expected?

In C 03, the std::vector::resize() function had only one overload that could initialize new elements with a specific value. C 11 introduced two overloads: resize(size_type n) for value initialization and resize(size_type n, const value_type &val) for copy initialization.

Boost.Container's boost::container::vector also supports value initialization in its resize() method. However, after testing the behavior of both C 11's std::vector and Boost.Container's boost::container::vector, it was observed that the final elements added to the vectors were still initialized with zeros, even though the intention was to use value initialization.

Understanding the Expected Behavior:

Value initialization means that new elements should be initialized with their default values (e.g., 0 for integers), while copy initialization means that new elements should be initialized with copies of the provided value.

Test Results:

The following code was used to test the behavior:

int main()
{
    // Initialize vectors
    std::vector<int> std_vec;
    bc::vector<int> boost_vec;
    bc::vector<int> boost_vec_default;
    // Fill and resize vectors
    init_vec(std_vec);
    init_vec(boost_vec);
    init_vec(boost_vec_default);
    // Resize vectors to 10 elements using different techniques
    std_vec.resize(10);
    boost_vec.resize(10);
    boost_vec_default.resize(10, bc::default_init);
    // Print results
    print_vec("std", std_vec);
    print_vec("boost", boost_vec);
    print_vec("boost w/default", boost_vec_default);
}
Copy after login

Compiling in C 03 Mode:

The output in C 03 mode is as expected. New elements in std_vec and boost_vec are initialized with zeros, while elements in boost_vec_default are default-initialized.

Compiling in C 11 Mode:

However, the output in C 11 mode shows that new elements in both std_vec and boost_vec are still being initialized with zeros. This indicates that the expected behavior of resize(size_type n) is not being met.

Explanation:

The reason for this unexpected behavior is that the compiler may be optimizing out the call to the value-initialization overload of resize(). To enforce value initialization, an allocator can be used that overrides the construct() method to perform value initialization explicitly.

The above is the detailed content of Does C 11's `std::vector::resize()` and Boost.Container's `boost::container::vector::resize()` Always Value-Initialize as Expected?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template