C++17 will bring us std::pmr::memory_resource
which is a clean interface for allocating and deallocating memory. Unlike the Allocator concept, it does just that and nothing more. There will also be std::pmr::polymorphic_allocator
which wraps a memory resource into a classical allocator so it can be used with existing containers.
If I'm about to write a new container (or other memory-hungry) type targeting C++17 and later, should I continue programming against the Allocator concept or rather use the newer and cleaner abstraction directly?
As of now, my thoughts go like this.
Reasons to continue using allocators:
std::pmr::*
container aliases continue to use allocators.std::pmr::polymorphic_allocator
, the allocator interface is more general and satisfies the needs of more clients.Reasons to start using memory resources instead of allocators:
std::pmr::memory_resource
interface is clean and straight-forward.std::pmr::polymorphic_allocator
. The other way round is more difficult.Do there already exist any recommendations for how to use the new library feature effectively?
At this point no.
Allocators in C++ currently are much easier than they used to be.
They provide both pmr (polymorphic) and classic allocator support.
More importantly, pmr based allocation has not been in heavy use for years. Any weaknesses may still come to light.
Fast pool based allocators, or even fixed buffer ones or sbo (small buffer optimization) extensions, may notice the virtualization overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With