Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


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:

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( ! );

// and deallocate is never null
assert( ! );

The polymorphic allocator is propagated recursively. Child elements of child elements will use the same memory resource as the parent.