value_from

## `value_from`

Convert an object of type `T` to `value`.

### Synopsis

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

    template<
        class T,
        class Context>
    void
    value_from(
        T&& t,
        Context const& ctx,
        value& jv); (1)

    template<
        class T,
        class Context>
    value
    value_from(
        T&& t,
        Context const& ctx,
        storage_ptr sp = {}); (2)

    template<
        class T>
    void
    value_from(
        T&& t,
        value& jv); (3)

    template<
        class T>
    value
    value_from(
        T&& t,
        storage_ptr sp = {}); (4)

### Description

This function attempts to convert an object of type `T` to `value` using

- one of `value`'s constructors,

- a library-provided generic conversion, or

- a user-provided overload of `tag_invoke`.

Out of the function supports default constructible types satisfying SequenceContainer, arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`, `std::optional`, `std::variant`, `std::nullptr_t`, and structs and enums described using Boost.Describe.

Conversion of other types is done by calling an overload of `tag_invoke` found by argument-dependent lookup. Its signature should be similar to:

    template< class FullContext >
    void tag_invoke( value_from_tag, value&, T, const Context&, const FullContext& );

or

    void tag_invoke( value_from_tag, value&, T, const Context& );

or

    void tag_invoke( value_from_tag, value&, T );

The overloads are checked for existence in that order and the first that matches will be selected.

The `ctx` argument can be used either as a tag type to provide conversions for third-party types, or to pass extra data to the conversion function.

Overloads (2) and (4) construct their return value using the `storage_ptr` `sp`, which ensures that the memory resource is correctly propagated.

### Exception Safety

Strong guarantee.

### Template Parameters

Type | Description
--- | ---
`T` | The type of the object to convert.
`Context` | The type of context passed to the conversion function.

### Return Value

Overloads (2) and (4) return `t` converted to `value`. Overloads (1) and 3 return `void` instead and pass their result via the out parameter `jv`.

### Parameters

Name | Description
--- | ---
`t` | The object to convert.
`ctx` | Context passed to the conversion function.
`jv` | `value` out parameter.
`sp` | A storage pointer referring to the memory resource to use for the returned `value`.

### See Also

`value_from_tag`, `value_to`, tag_invoke: A general pattern for supporting customisable functions

Convenience header <boost/json.hpp>

Edit this page