...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
To support code bases which are already using polymorphic allocators, the
containers in this library support std::uses_allocator
construction. For array
, object
, string
, and value
:
allocator_type
is an alias for a polymorphic_allocator
storage_ptr
will also accept
an instance of polymorphic_allocator
in the
same argument position.
get_allocator
returns an instance of polymorphic_allocator
constructed
from the memory_resource
used 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[0].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.