Summary
Allocators are a fundamentally arcane topic in C++, mainly for historical reasons. Several different interfaces, with different obscure use-cases, are piled one on top of the other; all of them involve intense metaprogramming; and vendor support for many of these features, even relatively old C++11 features such as fancy pointers, is still lacking.
C++17 offers the standard library type std::pmr::memory_resource
to clarify the existing distinction between memory resources (a.k.a. heaps) and allocators
(a.k.a. handles to heaps). Memory resources provide allocate
and deallocate
methods; allocators provide those methods as well as construct
and destroy
.
If you implement your own allocator type A
, it must be a template; its first template parameter should be the type T
that it expects to allocate
. Your allocator type A
must also have a templated constructor to support "rebinding" from A<U>
to A<T>
. Just like any other kind of pointer, an allocator type must support the ==
and...