Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

array

A value stores an instance of array as the underlying representation for a JSON array. Instances of the array type function identically to a std::vector of value. Additionally:

An empty array may be constructed without incurring any memory allocations using the default memory resource. A storage_ptr can also be explicitly specified:

array arr1; // empty array, uses the default memory resource

array arr2( make_shared_resource<monotonic_resource>() ); // empty array, uses a counted monotonic resource

Initializer lists can be used to construct objects with initial contents. These constructors may allocate memory and throw:

array arr( { "Hello", 42, true } );

Alternatively, elements may be inserted after construction:

array arr;

arr.emplace_back( "Hello" );
arr.emplace_back( 42 );
arr.emplace_back( true );

Similar to its standard library counterpart, elements may be accessed directly by their 0-based index with bounds checking using at, or without bounds checking using operator[]:

assert( arr[0].as_string() == "Hello" );

// The following line throws std::out_of_range, since the index is out of range
arr.at( 3 ) = nullptr;

For the complete listing of all available member functions and nested types, see the reference page for array.

Formatted Output

When an array is formatted to a std::ostream, the result is a valid JSON. That is, the array will be output with square brackets and the comma separated list of values, as per the JSON specification.


PrevUpHomeNext