Search code examples
c++containersallocator

Does allocator aware container need to replace the allocator even when they are equal?


When allocator's propagation policy tells to propagate the allocator, on container copy/move assignment or swap, do the container needs to do it even when allocators involved are equal?


Solution

  • As specified in the Allocator requirements, the propagate_on_container_copy_assignment type trait is or inherits from true_type only if an allocator of type X should be copied when the client container is copy-assigned. Similar wording is provided for the other two propagation policies, POCMA and POCS.

    Thus, the Standard does not specify whether the allocator should be propagated even if involved allocators compare equal.

    Since the copy assign, move assign and swap operations of the allocator are cheap and do not throw exceptions, many standard implementations, including libc++ and libstdc++, prefers to propagate the allocator unconditionally.