0

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

user17732522
  • 53,019
  • 2
  • 56
  • 105
ggg
  • 1,857
  • 2
  • 21
  • 33
  • 9
    `std::allocator::construct` is deprecated (and removed in C++20). That doesn't meant that you can't provide your own allocator with a custom `construct`. – Barry May 09 '22 at 21:54

1 Answers1

4

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.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577