Boost C++ Libraries

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

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

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( ! 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.


PrevUpHomeNext