Why are are std::allocator's construct and destroy functions deprecated in c++17?

后端 未结 2 705
萌比男神i
萌比男神i 2020-12-08 00:11

The c++17 specification deprecates the construct and destroy members of the std::allocator object. The working group provided rational

2条回答
  •  眼角桃花
    2020-12-08 01:05

    The allocator requirements table says that construct(c, args), if provided, must "construct an object of type C at c".

    It says absolutely nothing about 1) what arguments are to be passed to C's constructor or 2) how these arguments are to be passed. That's the allocator's choice, and in fact two allocators in the standard do mess with the arguments before passing them to C's constructor: std::scoped_allocator_adaptor and std::pmr::polymorphic_allocator. When constructing a std::pair, in particular, the arguments they pass to pair's constructor may not even resemble the ones they received.

    There's no requirement to perfectly forward, either; a C++03-style construct(T*, const T&) is conforming if inefficient.

    std::allocator's construct and destroy are deprecated because they are useless: no good C++11 and later code should ever call them directly, and they add nothing over the default.


    Handling memory alignment should be the task of allocate, not construct.

提交回复
热议问题