Making a container allocator-aware
Having covered memory resources (heaps) and allocators (handles to heaps), let's turn now to the third leg of the tripod: container classes. Inside each allocator-aware container, at least four things have to happen:
- The container instance must store an allocator instance as member data. (Therefore the container must take the type of the allocator as a template parameter; otherwise it can't know how much space to reserve for that member variable.)
- The container must provide constructors taking an allocator argument.
- The container must actually use its allocator to allocate and deallocate memory; every use of
new
ordelete
must be banished. - The container's move constructor, move assignment operator, and
swap
function must all propagate the allocator according to itsallocator_traits
.
Here is a very simple allocator-aware container--a container of just one single object, allocated on the heap. This is something like an allocator-aware version of std::unique_ptr...