...one of the most highly
regarded and expertly designed C++ library projects in the
world. — Herb Sutter and Andrei
To support code bases which are already using polymorphic allocators, the
containers in this library support
allocator_typeis an alias for a
storage_ptrwill also accept an instance of
polymorphic_allocatorin the same argument position.
get_allocatorreturns an instance of
polymorphic_allocatorconstructed from the
memory_resourceused by the container. Ownership of this memory resource is not transferred.
Practically, this means that when a library container type is used in a standard container that uses a polymorphic allocator, the allocator will propagate to the JSON type. For example:
// We want to use this resource for all the containers monotonic_resource mr; // Declare a vector of JSON values std::vector< value, polymorphic_allocator< value > > v( &mr ); // The polymorphic allocator will use our resource assert( v.get_allocator().resource() == &mr ); // Add a string to the vector v.emplace_back( "boost" ); // The vector propagates the memory resource to the string assert( v.storage().get() == &mr );
Library containers can be constructed from polymorphic allocators:
// This vector will use the default memory resource std::vector< value, polymorphic_allocator < value > > v; // This value will same memory resource as the vector value jv( v.get_allocator() ); // However, ownership is not transferred, assert( ! jv.storage().is_shared() ); // and deallocate is never null assert( ! jv.storage().is_deallocate_trivial() );
The polymorphic allocator is propagated recursively. Child elements of child elements will use the same memory resource as the parent.