value_ref

## `value_ref`

The type used in initializer lists.

### Synopsis

Defined in header <boost/json/value_ref.hpp>.

    class value_ref;

### Member Functions

Name | Description
--- | ---
`value_ref` [constructor] | Constructors.

### Description

This type is used in initializer lists for lazy construction of and assignment to the container types `value`, `array`, and `object`. The two types of initializer lists used are:

- `std::initializer_list< value_ref >` for constructing or assigning a `value` or `array`, and

- `std::initializer_list< std::pair< string_view, value_ref > >` for constructing or assigning an `object`.

A `value_ref` uses reference semantics. Creation of the actual container from the initializer list is lazily deferred until the list is used. This means that the `boost::container::pmr::memory_resource` used to construct a container can be specified after the point where the initializer list is specified. Also, the usage of this type allows to avoid constructing a `value` until it’s necessary.

### Example

This example demonstrates how a user-defined type containing a JSON value can be constructed from an initializer list:

    class my_type
    {
        value jv_;

    public:
        my_type( std::initializer_list<value_ref> init )
            : jv_(init)
        {
        }
    };

| `value_ref` does not take ownership of the objects it was constructed with. If those objects' lifetimes end before the `value_ref` object is used, you will get undefined behavior. Because of this it is advised against declaring a variable of type `std::initializer_list<value_ref>` except in function parameter lists.

### See Also

`value`, `array`, `object`, `value::set_at_pointer`.

Convenience header <boost/json.hpp>.

Edit this page