| Home | Libraries | People | FAQ | More

#### result_from_errno

Create `result` storing a portable error code.

##### Synopsis

Defined in header `<boost/json/system_error.hpp>`

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

##### Description

This function constructs a `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 `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`.

##### Parameters

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

##### Return Value

`error_code` with `value()` equal to `e` and `category()` equal to `boost::system::generic_category()`.

##### See Also

`try_value_to_tag`, `try_value_to`, `result_for`, `boost::system::generic_category`, `boost::source_location`.

Convenience header `<boost/json.hpp>`