result_from_errno

## `result_from_errno`

Create `boost::system::result` containing a portable error code.

### Synopsis

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

    template<
        class T>
    result_for< T, value >::type
    result_from_errno(
        int e,
        boost::source_location const* loc) noexcept;

### Description

This function constructs a `boost::system::result<T>` that stores `error_code` with `value()` equal to `e` and `category()` equal to `boost::system::generic_category()`.

The main use for this function is in implementation of functions returning `boost::system::result`, without including `boost/json/system_error.hpp` or even `<system_error>`. In particular, it may be useful for customizations of `try_value_to` without creating a physical dependency on Boost.JSON. For example:

    #include <cerrno>
    #include <boost/assert/source_location.hpp>

    namespace boost {
    namespace json {

    class value;

    template<class T>
    struct try_value_to_tag;

    template<class T1, class T2>
    struct result_for;

    template <class T>
    typename result_for<T, value>::type
    result_from_errno(int e, boost::source_location const* loc) noexcept

    }
    }

    namespace mine {

    class my_class;
    ...
    template<class JsonValue>
    boost::json::result_for<my_class, JsonValue>
    tag_invoke(boost::json::try_value_to_tag<my_class>, const JsonValue& jv)
    {
        BOOST_STATIC_CONSTEXPR boost::source_location loc = BOOST_CURRENT_LOCATION;
        if( !jv.is_null() )
            return boost::json::result_from_errno<my_class>(EINVAL, &loc);
        return my_class();
    }

    }

### Exception Safety

Does not throw exceptions.

### Template Parameters

Type | Description
--- | ---
`T` | The value type of returned `result`.

### Return Value

A `boost::system::result` containing an error.

### Parameters

Name | Description
--- | ---
`e` | The error value.
`loc` | The error location.

### See Also

`try_value_to_tag`, `try_value_to`, `result_for`.

Convenience header <boost/json.hpp>

Edit this page