AllocatorAwareContainer and copy-and-swap idiom

Viewed 96

I'm trying to learn data structures along with idiomatic C++ and because of stuff like this I feel like it's probably not the best way to learn either. Let's say I wanted to implement a dynamic array class which would meet the Container, ReversibleContainer and AllocatorAwareContainer named requirements.

The cppreference page on AllocatorAwareContainer says:

The only way to replace an allocator is copy-assignment, move-assignment, and swap:

  1. Copy-assignment will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value is true
  2. Move-assignment will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value is true
  3. Swap will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_swap::value is true. Specifically, it will exchange the allocator instances through an unqualified call to the non-member function swap, see Swappable.

Note: swapping two containers with unequal allocators if propagate_on_container_swap is false is undefined behavior.

What if I implement both of those assignment operators using copy-and-swap idiom? I mean, there aren't that many ways to check propagate_on_container_copy_assignment and propagate_on_container_move_assignment if they are expressed in terms of move/copy constructor and a swap function. Is it safe to assume that when propagate_on_container_swap is true, the remaining two are true as well?

0 Answers
Related