To my understanding, a Container should have an Allocator member data, which is used internally.
Question: what happens, for example, when we swap() two containers that use different allocators?
Must the allocators be swapped as well?
Must all elements of the container be reallocated when the allocator is changed?
I assume that if the allocator is replaced, then the elements must be destroyed, deallocated and then allocated and constructed all over again.
Is this correct?
Also, regarding this:
cppreference wiki wrote:
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.
> I assume that if the allocator is replaced, then the elements must be destroyed, deallocated
> and then constructed and allocated all over again.
> Is this correct?
No. All standard containers are moveable (std::array<> implicitly so).
Internally swap would just swap pointers to the memory holding the values.
Before swap:
1 2 3 4
Container ONE Container TWO
Allocator A1 Allocator A2
Pointer(s) to values allocated by A1 Pointer(s) to values allocated by A2
After swap:
1 2 3 4
Container ONE Container TWO
Allocator A2 Allocator A1
Pointer(s) to values allocated by A2 Pointer(s) to values allocated by A1
> Why unqualified?
Facilitate ADL (argument dependent name lookup) for the swap() function.
So the pointer type remains the one given by the original allocator, and we convert the new pointers to it.
Three questions: 1) What does // slightly simplified mean; is there more to do? 2) Why did you use std::nullptr_t instead of nullptr? 3) Why exactly is using dumb pointers a bad approach?
Don't worry too much about this. One can be a very competent C++ programmer without ever knowing the gory details about the implementation of strictly conforming allocators and allocator aware containers.