'Modern C++ containers with default initialization

Is it still possible to change the initialization in C++ containers to default initialization instead of value initialization? There is a method below that modifies the allocator to change it, but std::allocator<T>::construct is deprecated in C++17, also it doesn't work with PMR allocators.

Is this behavior of vector::resize(size_type n) under C++11 and Boost.Container correct?

https://en.cppreference.com/w/cpp/memory/allocator/construct



Solution 1:[1]

Adding a member construct to your custom allocator is still the correct way to get this customization.

The only reason it was deprecated/removed in std::allocator is because std::allocator doesn't need this customization. std::allocator_traits::construct does the right thing for std::allocator. It was left in for awhile for backwards compatibility with code that directly called std::allocator<T>::construct instead of std::allocator_traits<std::allocator>::construct.

C++20 forces clients to use the more modern std::allocator_traits<std::allocator>::construct. And clients of your custom allocator should also access your construct via allocator_traits.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Howard Hinnant