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 a snapshot of the master branch, built from commit d4f4f87d35.
PrevUpHomeNext

Allocation control

As value_from creates a value object, users may want to control the way memory is allocated for it. For this reason the function has an optional storage_ptr parameter, that is used to set the memory_resource for the result.

[Note] Note

value_to does not have a similar parameter, as value is not created.

As the conversion result is set via an output parameter of type value&, the intended storage_ptr is correctly propagated. But users still should take care to not create temporaries using the default memory_resource by accident.

For example, consider this alternative implementation of tag_invoke for ip_address from the section Custom conversions.

void
tag_invoke( const value_from_tag&, value& jv, ip_address const& addr )
{
    jv = array{ b[0], b[1], b[2], b[3] };
}

This implementation explicitly creates an array rather than relying on assignment from an initializer list. But the array uses default memory_resource, not the one used by jv.

To avoid creating such temporaries with an incorrect memory_resource, using value's member functions emplace_array, emplace_object, and emplace_string can be helpful.


PrevUpHomeNext