Uses-allocator Construction
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
:
-
The nested type
allocator_type
is an alias for apolymorphic_allocator
. -
All eligible constructors which accept
storage_ptr
will also accept an instance ofpolymorphic_allocator
in the same argument position. -
The member function
get_allocator
returns an instance ofpolymorphic_allocator
constructed from thememory_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, boost::container::pmr::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, boost::container::pmr::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.