Polymorphic Allocator
Explore how polymorphic allocators enhance C++17 memory management by using memory resource objects for flexible allocation strategies. Understand key components like std::pmr::memory_resource and predefined memory resources, and see practical examples with polymorphic containers such as std::pmr::vector. Gain the knowledge to manage memory efficiently in modern C++ applications.
We'll cover the following...
Polymorphic Allocator
To be concise, a polymorphic allocator conforms to the rules of an allocator from the Standard Library.
However, at its core, it uses a memory resource object to perform memory management.
Polymorphic Allocator contains a pointer to a memory resource class, and that’s why it can use a virtual method dispatch. You can change the memory resource at runtime while keeping the type of the allocator.
All the types for polymorphic allocators live in a separate namespace std::pmr (PMR stands for Polymorphic Memory Resource), in the <memory_resource> header.
Core elements of pmr:
std::pmr::memory_resource- is an abstract base class for all other implementations. It defines the following pure virtual methods: