sumty
0.1.0
Better sum types for C++
|
Type that contains an ok value, or an error. More...
#include <sumty/result.hpp>
Public Types | |
using | value_type = ... |
using | reference = ... |
using | const_reference = ... |
using | rvalue_reference = ... |
using | const_rvalue_reference = ... |
using | pointer = ... |
using | const_pointer = ... |
using | error_type = ... |
using | error_reference = ... |
using | error_const_reference = ... |
using | error_rvalue_reference = ... |
using | error_const_rvalue_reference = ... |
using | error_pointer = ... |
using | error_const_pointer = ... |
template<typename U > | |
using | rebind = result< U, E > |
template<typename V > | |
using | rebind_error = result< T, V > |
using | unexpected_type = result< never, E > |
Public Member Functions | |
constexpr | result () CONDITIONALLY_NOEXCEPT |
Default constructor. More... | |
constexpr | result (const result &) CONDITIONALLY_NOEXCEPT |
Copy constructor. More... | |
constexpr | result (result &&) CONDITIONALLY_NOEXCEPT |
Move constructor. More... | |
template<typename... Args> | |
constexpr CONDITONALLY_EXPLICIT | result ([[maybe_unused]] std::in_place_t inplace, Args &&... args) |
Emplacement constructor. More... | |
template<typename U , typename... Args> | |
constexpr | result ([[maybe_unused]] std::in_place_t inplace, std::initializer_list< U > init, Args &&... args) |
Emplacement constructor with initializer list. More... | |
template<typename... Args> | |
constexpr CONDITIONALLY_EXPLICIT | result (std::in_place_index_t< 0 > inplace, Args &&... args) |
Emplacement constructor. More... | |
template<typename U , typename... Args> | |
constexpr | result (std::in_place_index_t< 0 > inplace, std::initializer_list< U > init, Args &&... args) |
Emplacement constructor with initializer list. More... | |
template<typename... Args> | |
constexpr CONDITONALLY_EXPLICIT | result (in_place_error_t inplace, Args &&... args) |
Error emplacement constructor. More... | |
template<typename U , typename... Args> | |
constexpr | result (in_place_error_t inplace, std::initializer_list< U > init, Args &&... args) |
Error emplacement constructor with initializer list. More... | |
template<typename U > | |
constexpr CONDITIONALLY_EXPLICIT | result (U &&value) |
Forwarding constructor. More... | |
template<typename U , typename V > | |
constexpr CONDITIONALLY_EXPLICIT | result (const result< U, V > &other) |
Converting copy constructor. More... | |
template<typename U , typename V > | |
constexpr CONDITIONALLY_EXPLICIT | result (result< U, V > &&other) |
Converting move constructor. More... | |
constexpr | ~result () CONDITIONALLY_NOEXCEPT |
Destructor. More... | |
constexpr result & | operator= (const result &) CONDITIONALLY_NOEXCEPT |
Copy assignment operator. More... | |
constexpr result & | operator= (result &&) CONDITIONALLY_NOEXCEPT |
Move assignment operator. More... | |
template<typename U > | |
constexpr result< T, E > & | operator= (U &&value) |
Value assignment operator. More... | |
constexpr | operator bool () const noexcept |
Implicit conversion to bool . More... | |
constexpr bool | has_value () const noexcept |
Returns true if the result contains an ok value. More... | |
constexpr bool | is_ok () const noexcept |
Returns true if the result contains an ok value. More... | |
constexpr bool | is_error () const noexcept |
Returns true if the result contains an error value. More... | |
constexpr reference | operator* () &noexcept |
Accesses the ok value contained in the result. More... | |
constexpr const_reference | operator* () const &noexcept |
Accesses the ok value contained in the result. More... | |
constexpr rvalue_reference | operator* () && |
Accesses the ok value contained in the result. More... | |
constexpr const_rvalue_reference | operator* () const && |
Accesses the ok value contained in the result. More... | |
constexpr pointer | operator-> () noexcept |
Accesses members of the ok value contained in the result. More... | |
constexpr const_pointer | operator-> () const noexcept |
Accesses members of the ok value contained in the result. More... | |
constexpr reference | value () & |
Accesses the ok value contained in the result. More... | |
constexpr const_reference | value () const & |
Accesses the ok value contained in the result. More... | |
constexpr rvalue_reference | value () && |
Accesses the ok value contained in the result. More... | |
constexpr rvalue_reference | value () const && |
Accesses the ok value contained in the result. More... | |
constexpr error_reference | error () &noexcept |
Accesses the error value contained in the result. More... | |
constexpr error_const_reference | error () const &noexcept |
Accesses the error value contained in the result. More... | |
constexpr error_rvalue_reference | error () && |
Accesses the error value contained in the result. More... | |
constexpr error_const_rvalue_reference | error () const && |
Accesses the error value contained in the result. More... | |
constexpr result< never, E > | prop_error () const & |
Extracts the error in order to propagate it. More... | |
constexpr result< never, E > | prop_error () && |
Extracts the error in order to propagate it. More... | |
template<typename U > | |
constexpr value_type | value_or (U &&default_value) const & |
Gets the ok value, or a default if the result is an error. More... | |
template<typename U > | |
constexpr value_type | value_or (U &&default_value) && |
Gets the ok value, or a default if the result is an error. More... | |
template<typename F > | |
constexpr value_type | value_or_else (F &&f) const & |
Gets the ok value, or returns the result of a callable. More... | |
template<typename F > | |
constexpr value_type | value_or_else (F &&f) && |
Gets the ok value, or returns the result of a callable. More... | |
constexpr reference | ok () & |
Accesses the ok value contained in the result. More... | |
constexpr const_reference | ok () const & |
Accesses the ok value contained in the result. More... | |
constexpr rvalue_reference | ok () && |
Accesses the ok value contained in the result. More... | |
constexpr rvalue_reference | ok () const && |
Accesses the ok value contained in the result. More... | |
template<typename U > | |
constexpr value_type | ok_or (U &&default_value) const & |
Gets the ok value, or a default if the result is an error. More... | |
template<typename U > | |
constexpr value_type | ok_or (U &&default_value) && |
Gets the ok value, or a default if the result is an error. More... | |
template<typename F > | |
constexpr value_type | ok_or_else (F &&f) const & |
Gets the ok value, or returns the result of a callable. More... | |
template<typename F > | |
constexpr value_type | ok_or_else (F &&f) && |
Gets the ok value, or returns the result of a callable. More... | |
template<typename F > | |
constexpr auto | and_then (F &&f) & |
Applies a callable to the contents of a result. More... | |
template<typename F > | |
constexpr auto | and_then (F &&f) const & |
Applies a callable to the contents of a result. More... | |
template<typename F > | |
constexpr auto | and_then (F &&f) && |
Applies a callable to the contents of a result. More... | |
template<typename F > | |
constexpr auto | and_then (F &&f) const && |
Applies a callable to the contents of a result. More... | |
template<typename F > | |
constexpr auto | transform (F &&f) & |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | transform (F &&f) const & |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | transform (F &&f) && |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | transform (F &&f) const && |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | map (F &&f) & |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | map (F &&f) const & |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | map (F &&f) && |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr auto | map (F &&f) const && |
Perforams a transformation on the ok value of a result. More... | |
template<typename F > | |
constexpr result | or_else (F &&f) const & |
Returns the result of invoking f if the result is an error. More... | |
template<typename F > | |
constexpr result | or_else (F &&f) && |
Returns the result of invoking f if the result is an error. More... | |
constexpr auto | flatten () const & |
Converts result<result<T, E1>, E2> into result<T, E3> More... | |
constexpr auto | flatten () && |
Converts result<result<T, E1>, E2> into result<T, E3> More... | |
constexpr auto | flatten_all () const & |
Converts nested result types into a single result. More... | |
constexpr auto | flatten_all () && |
Converts nested result types into a single result. More... | |
constexpr auto | transpose () const & |
Converts result<option<T>, E> into option<result<T, E>> More... | |
constexpr auto | transpose () && |
Converts result<option<T>, E> into option<result<T, E>> More... | |
constexpr result< E, T > | invert () const & |
Swaps the ok and error types of a result. More... | |
constexpr result< E, T > | invert () && |
Swaps the ok and error types of a result. More... | |
template<typename F > | |
constexpr auto | transform_error (F &&f) & |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | transform_error (F &&f) const & |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | transform_error (F &&f) && |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | transform_error (F &&f) const && |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | map_error (F &&f) & |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | map_error (F &&f) const & |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | map_error (F &&f) && |
Perforams a transformation on the error value of a result. More... | |
template<typename F > | |
constexpr auto | map_error (F &&f) const && |
Perforams a transformation on the error value of a result. More... | |
constexpr result< reference, error_reference > | ref () noexcept |
Converts a result reference into a result of references. More... | |
constexpr result< const_reference, error_const_reference > | ref () const noexcept |
Converts a result reference into a result of references. More... | |
constexpr result< const_reference, error_const_reference > | cref () const noexcept |
Converts a result reference into a result of references. More... | |
constexpr option< T > | or_none () const &noexcept |
Discards error values and converts into an option. More... | |
constexpr option< T > | or_none () && |
Discards error values and converts into an option. More... | |
constexpr option< T > | ok_or_none () const &noexcept |
Discards error values and converts into an option. More... | |
constexpr option< T > | ok_or_none () && |
Discards error values and converts into an option. More... | |
constexpr option< E > | error_or_none () const &noexcept |
Discards ok value and converts into an option. More... | |
constexpr option< E > | error_or_none () && |
Discards ok value and converts into an option. More... | |
template<typename... Args> | |
constexpr reference | emplace (Args &&... args) |
Constructs a new ok value in place into the result. More... | |
template<typename U , typename... Args> | |
constexpr reference | emplace (std::initializer_list< U > ilist, Args &&... args) |
Constructs a new ok value in place into the result. More... | |
template<typename... Args> | |
constexpr error_reference | emplace_error (Args &&... args) |
Constructs a new error value in place into the result. More... | |
template<typename U , typename... Args> | |
constexpr error_reference | emplace_error (std::initializer_list< U > ilist, Args &&... args) |
Constructs a new error value in place into the result. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) & |
Invokes a visitor witht he contained value. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) const & |
Invokes a visitor witht he contained value. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) && |
Invokes a visitor witht he contained value. More... | |
template<typename V > | |
constexpr DEDUCED | visit (V &&visitor) const && |
Invokes a visitor witht he contained value. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) & |
Invokes a visitor with the contained value and meta data. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) const & |
Invokes a visitor with the contained value and meta data. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) && |
Invokes a visitor with the contained value and meta data. More... | |
template<typename V > | |
constexpr DEDUCED | visit_informed (V &&visitor) const && |
Invokes a visitor with the contained value and meta data. More... | |
constexpr void | swap (result &other) CONDITIONALLY_NOEXCEPT |
Swaps two result instances. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<size_t IDX, typename T , typename E > | |
constexpr REFERENCE | get (result< T, E > &res) |
Gets a result value by index, as if it were a variant. More... | |
template<size_t IDX, typename T , typename E > | |
constexpr CONST_REFERENCE | get (const result< T, E > &res) |
Gets a result value by index, as if it were a variant. More... | |
template<size_t IDX, typename T , typename E > | |
constexpr RVALUE_REFERENCE | get (result< T, E > &&res) |
Gets a result value by index, as if it were a variant. More... | |
template<size_t IDX, typename T , typename E > | |
constexpr CONST_RVALUE_REFERENCE | get (const result< T, E > &&res) |
Gets a result value by index, as if it were a variant. More... | |
template<typename U , typename T , typename E > | |
constexpr REFERENCE | get (result< T, E > &res) |
Gets a result value by type, as if it were a variant. More... | |
template<typename U , typename T , typename E > | |
constexpr CONST_REFERENCE | get (const result< T, E > &res) |
Gets a result value by type, as if it were a variant. More... | |
template<typename U , typename T , typename E > | |
constexpr RVALUE_REFERENCE | get (result< T, E > &&res) |
Gets a result value by type, as if it were a variant. More... | |
template<typename U , typename T , typename E > | |
constexpr CONST_RVALUE_REFERENCE | get (const result< T, E > &&res) |
Gets a result value by type, as if it were a variant. More... | |
template<typename T , typename E , typename U , typename V > | |
constexpr bool | operator== (const result< T, E > &lhs, const result< U, V > &rhs) |
Compares two result instances for equality. More... | |
template<typename T , typename E , typename U > | |
constexpr bool | operator== (const result< T, E > &lhs, const U &rhs) |
Compares a result with a plain value for equality. More... | |
template<typename T , typename E , typename U > | |
constexpr bool | operator== (const U &lhs, const result< T, E > &rhs) |
Compares a result with a plain value for equality. More... | |
template<typename T , typename E > | |
constexpr void | swap (result< T, E > &a, result< T, E > &b) CONDITIONALLY_NOEXCEPT |
Swaps two result instances. More... | |
template<typename T , typename... Args> | |
constexpr result< T, never > | ok (Args &&... args) |
Creates an ok valued result, constructed in place. More... | |
template<typename T , typename U , typename... Args> | |
constexpr result< T, never > | ok (std::initializer_list< U > ilist, Args &&... args) |
Creates an ok valued result, constructed in place. More... | |
template<typename E , typename... Args> | |
constexpr result< never, E > | error (Args &&... args) |
Creates an error valued result, constructed in place. More... | |
template<typename E , typename U , typename... Args> | |
constexpr result< never, E > | error (std::initializer_list< U > ilist, Args &&... args) |
Creates an error valued result, constructed in place. More... | |
![]() | |
constexpr REFERENCE | get (variant< T... > &v) |
Gets a variant alternative by index. More... | |
constexpr CONST_REFERENCE | get (const variant< T... > &v) |
Gets a variant alternative by index. More... | |
constexpr RVALUE_REFERENCE | get (variant< T... > &&v) |
Gets a variant alternative by index. More... | |
constexpr CONST_RVALUE_REFERENCE | get (const variant< T... > &&v) |
Gets a variant alternative by index. More... | |
constexpr REFERENCE | get (variant< U... > &v) |
Gets a variant alternative by type. More... | |
constexpr CONST_REFERENCE | get (const variant< U... > &v) |
Gets a variant alternative by type. More... | |
constexpr RVALUE_REFERENCE | get (variant< U... > &&v) |
Gets a variant alternative by type. More... | |
constexpr CONST_RVALUE_REFERENCE | get (const variant< U... > &&v) |
Gets a variant alternative by type. More... | |
constexpr POINTER | get_if (variant< T... > &v) noexcept |
Gets a variant alternative pointer by index if the variant holds it. More... | |
constexpr CONST_POINTER | get_if (const variant< T... > &v) noexcept |
Gets a variant alternative pointer by index if the variant holds it. More... | |
constexpr POINTER | get_if (variant< U... > &v) noexcept |
Gets a variant alternative pointer by type if the variant holds it. More... | |
constexpr CONST_POINTER | get_if (const variant< U... > &v) noexcept |
Gets a variant alternative pointer by type if the variant holds it. More... | |
constexpr bool | holds_alternative (const variant< U... > &v) noexcept |
Checks if a variant contains a particular alternative. More... | |
constexpr DEDUCED | visit (V &&visitor) |
Calls a visitor callable with the contained variant alternatives. More... | |
constexpr DEDUCED | visit (V &&visitor, T0 &&var0, TN &&... varn) |
Calls a visitor callable with the contained variant alternatives. More... | |
constexpr void | swap (variant< T... > &a, variant< T... > &b) CONDITIONALLY_NOEXCEPT |
Swaps two variant instances. More... | |
Type that contains an ok value, or an error.
result is a reimplementation of std::expected
with several improvements. The key difference is that references (lvalue and rvalue) are can be used for both the value type and the error type, and void
can be used for the error type (std::expected
already allows void
for the value type).
Internally, result<T, E>
is represented as a variant<T, E>. Thus, result benefits from the size optimizations implemented by variant (see variant documentation for details). A couple special case result size examples are shown in the example below.
In practice, the benefit of result over std::expected
is that result can be used in more places, especially with generic code. A generic function (function template) that wants to be able to return a value of any type, but also allow that return value to instead communicate an error on failure can simply return a result<T, E>
, where T
is now allowed to be a reference or even void
(and so is E
, for that matter).
The power of result is also enhanced when used in combination with error_set. error_set makes it easy to represent a set of different error possibilities of different types in a single value, and simplifies error propagation when used with result. See the documentation of error_set for more details and examples.
|
constexpr |
Default constructor.
Initializes the result with a default constructed ok value.
Copy constructor.
If the source result has an ok value, the new result is initialized with a copy constructed ok value. If the source result has an error value, the new result is initialized with a copy constructed error.
Move constructor.
If the source result has an ok value, the new result is initialized with a move constructed ok value. If the source result has an error value, the new result is initialized with a move constructed error.
|
inlineconstexpr |
Emplacement constructor.
The result is initialized such that an ok value is constructed in place from the forwarded arguments.
This constructor is explicit
if inplace
is the only argument.
|
inlineconstexpr |
Emplacement constructor with initializer list.
The result is initialized such that an ok value is constructed in place from the forwarded arguments.
|
inlineconstexpr |
Emplacement constructor.
The result is initialized such that an ok value is constructed in place from the forwarded arguments.
This constructor is explicit
if inplace
is the only argument.
|
inlineconstexpr |
Emplacement constructor with initializer list.
The result is initialized such that an ok value is constructed in place from the forwarded arguments.
|
inlineconstexpr |
Error emplacement constructor.
The result is initialized such that an error value is constructed in place from the forwarded arguments.
|
inlineconstexpr |
Error emplacement constructor with initializer list.
The result is initialized such that an error value is constructed in place from the forwarded arguments.
|
inlineconstexpr |
Forwarding constructor.
The result is initialized such that it contains an ok value that is constructed in place from the forwarded value.
This constructor only participates in overload resolution if the ok value is constructible from the forwarded value, the forwarded value is not of type std::in_place_t
, sumty::in_place_t
, std::in_place_index_t<0>
, sumty::in_place_index_t<0>
, std::in_place_index_t<1>
, sumty::in_place_index_t<1>
, or sumty::in_place_error_t
, and either the ok value type is a scalar or the forwarded value is not a result instance.
This constructor is explicit
if the forwarded value is not implicitly convertible to T
.
Converting copy constructor.
This constructor converts a result with one pair of ok and error types into a
This constructor only particpates in overload resolution if the ok type T
of the destination result is constructible from the ok type U
of the source result, and the error type E
of the destiantion result is constructible from the error type V
of the source result.
This constructor is explicit
if either U
or V
is not implicitly convertible to T
or E
, respectively.
Converting move constructor.
This constructor converts a result with one pair of ok and error types into a
This constructor only particpates in overload resolution if the ok type T
of the destination result is constructible from the moved ok type U
of the source result, and the error type E
of the destiantion result is constructible from the move error type V
of the source result.
This constructor is explicit
if either U
or V
is not implicitly convertible to T
or E
, respectively.
|
constexpr |
Destructor.
Destroys the contained value in place.
The desctructor is noexcept
if both T
and E
are nothrow destructible.
|
inlineconstexpr |
Applies a callable to the contents of a result.
If the result contains an ok value, the value is passed into the callable, and the result of invoking the callable is returned from this function. If the result contains an error, the callable is not invoked, and a copy of the error is returned.
This function only participates in overload resultion if the result of invoking the callable is a result.
|
inlineconstexpr |
Applies a callable to the contents of a result.
If the result contains an ok value, the value is passed into the callable, and the result of invoking the callable is returned from this function. If the result contains an error, the callable is not invoked, the error is returned.
This function only participates in overload resultion if the result of invoking the callable is a result.
|
inlineconstexpr |
Applies a callable to the contents of a result.
If the result contains an ok value, the value is passed into the callable, and the result of invoking the callable is returned from this function. If the result contains an error, the callable is not invoked, and a copy of the error is returned.
This function only participates in overload resultion if the result of invoking the callable is a result.
|
inlineconstexpr |
Applies a callable to the contents of a result.
If the result contains an ok value, the value is passed into the callable, and the result of invoking the callable is returned from this function. If the result contains an error, the callable is not invoked, the error is returned.
This function only participates in overload resultion if the result of invoking the callable is a result.
|
inlineconstexprnoexcept |
Converts a result reference into a result of references.
|
inlineconstexpr |
Constructs a new ok value in place into the result.
The value previously contained in the result, ok or error, is destroyed in place. The new ok value is then constructed in place as if by placement new.
|
inlineconstexpr |
Constructs a new ok value in place into the result.
The value previously contained in the result, ok or error, is destroyed in place. The new ok value is then constructed in place as if by placement new.
|
inlineconstexpr |
Constructs a new error value in place into the result.
The value previously contained in the result, ok or error, is destroyed in place. The new error value is then constructed in place as if by placement new.
|
inlineconstexpr |
Constructs a new error value in place into the result.
The value previously contained in the result, ok or error, is destroyed in place. The new error value is then constructed in place as if by placement new.
|
inlineconstexpr |
Accesses the error value contained in the result.
This function does not check if the result contains an ok value. Use of this function when the result contains an error results in undefined behavior.
|
inlineconstexprnoexcept |
|
inlineconstexpr |
Accesses the error value contained in the result.
This function does not check if the result contains an ok value. Use of this function when the result contains an error results in undefined behavior.
|
inlineconstexprnoexcept |
Accesses the error value contained in the result.
This function does not check if the result contains an ok value. Use of this function when the result contains an error results in undefined behavior.
|
inlineconstexpr |
Discards ok value and converts into an option.
If the result contains an error value, that value is returned wrapped in an option. If the result contains an ok value, none
is returned.
|
inlineconstexprnoexcept |
Discards ok value and converts into an option.
If the result contains an error value, a copy of that value is returned wrapped in an option. If the result contains an ok value, none
is returned.
|
inlineconstexpr |
Converts result<result<T, E1>, E2>
into result<T, E3>
If the outer result contains an error, that error is returned. If the outer result contains an ok value, and the inner result contains an error, that inner error is returned. If the outer result contains an ok value and the inner result contains an ok value, that inner ok value is returned.
If the outer result and the inner result have different error types, the returned result will be one of these two types based on the following priority rules:
void
, use the inner error typevoid
, use the outer error typeNote that only one level of result is removed. For example, a result<result<result<T, E>, E>, E>
will flatten to a result<result<T, E>, E>
.
|
inlineconstexpr |
Converts result<result<T, E1>, E2>
into result<T, E3>
If the outer result contains an error, a copy of that error is returned. If the outer result contains an ok value, and the inner result contains an error, a copy of that inner error is returned. If the outer result contains an ok value and the inner result contains an ok value, a copy of that inner ok value is returned.
If the outer result and the inner result have different error types, the returned result will be one of these two types based on the following priority rules:
void
, use the inner error typevoid
, use the outer error typeNote that only one level of result is removed. For example, a result<result<result<T, E>, E>, E>
will flatten to a result<result<T, E>, E>
.
|
inlineconstexpr |
Converts nested result types into a single result.
Like flatten, this function removes nested layers of result, such as converting result<result<T, E>, E>
into result<T, E>
. However, unlike flatten, this function removes all layers of nesting and works for unnested result types. For example, result<result<result<T, E>, E, E>
is converted directly to result<T, E>
, and for result<T, E>
, where T
is not a result type, no layers of result are removed.
|
inlineconstexpr |
Converts nested result types into a single result.
Like flatten, this function removes nested layers of result, such as converting result<result<T, E>, E>
into result<T, E>
. However, unlike flatten, this function removes all layers of nesting and works for unnested result types. For example, result<result<result<T, E>, E, E>
is converted directly to result<T, E>
, and for result<T, E>
, where T
is not a result type, no layers of result are removed.
|
inlineconstexprnoexcept |
Returns true
if the result contains an ok value.
|
inlineconstexpr |
Swaps the ok and error types of a result.
If the result contains an ok value, a copy of the ok value will be returned as an error. If the result contains an error value, the error value will be returned as an ok value.
|
inlineconstexpr |
Swaps the ok and error types of a result.
If the result contains an ok value, a copy of the ok value will be returned as an error. If the result contains an error value, the error value will be returned as an ok value.
|
inlineconstexprnoexcept |
Returns true
if the result contains an error value.
|
inlineconstexprnoexcept |
Returns true
if the result contains an ok value.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, a copy of the error is returned.
Note that this function is idential to transform, but the name map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, the error is returned.
Note that this function is idential to transform, but the name map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, a copy of the error is returned.
Note that this function is idential to transform, but the name map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, the error is returned.
Note that this function is idential to transform, but the name map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, a copy of the ok value is returned.
Note that this function is identical to transform_error, but the name map_error
is more typical for this sort of monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, the ok value is returned.
Note that this function is identical to transform_error, but the name map_error
is more typical for this sort of monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, a copy of the ok value is returned.
Note that this function is identical to transform_error, but the name map_error
is more typical for this sort of monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, the ok value is returned.
Note that this function is identical to transform_error, but the name map_error
is more typical for this sort of monadic operation outside of C++.
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Gets the ok value, or a default if the result is an error.
If the result contains an ok value, that ok value is returned. If the result contains an error value, the provided default value is forwarded, cast, and returned as by the following: static_cast<T>(std::forward<U>(default_value))
.
|
inlineconstexpr |
Gets the ok value, or a default if the result is an error.
If the result contains an ok value, a copy of that ok value is returned. If the result contains an error value, the provided default value is forwarded, cast, and returned as by the following: static_cast<T>(std::forward<U>(default_value))
.
|
inlineconstexpr |
Gets the ok value, or returns the result of a callable.
If the result contains an ok value, a copy of that ok value is returned. If the result contains an error, the provided callable is invoked and the result is cast and returned, as if by: static_cast<T>(std::invoke(std::forward<F>(f)))
.
|
inlineconstexpr |
Gets the ok value, or returns the result of a callable.
If the result contains an ok value, a copy of that ok value is returned. If the result contains an error, the provided callable is invoked and the result is cast and returned, as if by: static_cast<T>(std::invoke(std::forward<F>(f)))
.
|
inlineconstexpr |
Discards error values and converts into an option.
If the result contains an ok value, that value is returned wrapped in an option. If the result contains an error, none
is returned.
|
inlineconstexprnoexcept |
Discards error values and converts into an option.
If the result contains an ok value, a copy of that value is returned wrapped in an option. If the result contains an error, none
is returned.
|
inlineconstexprnoexcept |
Implicit conversion to bool
.
This implicit conversion allows a result to be used directly in a condition to check if the result contains an ok value.
|
inlineconstexpr |
|
inlineconstexprnoexcept |
|
inlineconstexpr |
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
|
inlineconstexprnoexcept |
Copy assignment operator.
The destination result is reassigned such that it will now contain a copy of the contained value of the source result, which may be either the ok type or the error type.
If both the source and destination contain the ok type, or both contain the error type, the values are copy assigned directly. Otherwise, the old value in the destination is destroyed and the new value is copy constructed.
This function is noexcept
if both T
and E
are all of the following:
Move assignment operator.
The destination result is reassigned such that it will now contain the moved contained value of the source result, which may be either the ok type or the error type.
If both the source and destination contain the ok type, or both contain the error type, the values are move assigned directly. Otherwise, the old value in the destination is destroyed and the new value is move constructed.
This function is noexcept
if both T
and E
are all of the following:
|
inlineconstexpr |
Value assignment operator.
Sets the result to contain the forwarded ok value, converted to T
. If the result already contains an ok value, the forwarded value is assigned directly to the contained value. Otherwise, the error type is destroyed, and the ok type is constructed from the forwarded value.
This function only participates in overload resolution if:
T
is constructible from U
T
is assignable from U
|
inlineconstexpr |
Returns the result of invoking f
if the result is an error.
If the result contains an ok value, the entire result is returned. If the result contains an error value, f
is invoked with the error value as an argument, and the returned value from invoking f
is returned from this function, converting to result<T, E>
if necessary.
|
inlineconstexpr |
Returns the result of invoking f
if the result is an error.
If the result contains an ok value, a copy of the entire result is returned. If the result contains an error value, f
is invoked with the error value as an argument, and the returned value from invoking f
is returned from this function, converting to result<T, E>
if necessary.
|
inlineconstexpr |
Discards error values and converts into an option.
If the result contains an ok value, that value is returned wrapped in an option. If the result contains an error, none
is returned.
|
inlineconstexprnoexcept |
Discards error values and converts into an option.
If the result contains an ok value, a copy of that value is returned wrapped in an option. If the result contains an error, none
is returned.
Extracts the error in order to propagate it.
This function returns the error value by itself. This value can then be converted into a different result type for the purpose of propagating the error.
This function does not check if the result contains an ok value. Use of this function when the result contains an error results in undefined behavior.
Extracts the error in order to propagate it.
This function returns the error value by itself. This value can then be converted into a different result type for the purpose of propagating the error.
This function does not check if the result contains an ok value. Use of this function when the result contains an error results in undefined behavior.
|
inlineconstexprnoexcept |
Converts a result reference into a result of references.
|
inlineconstexprnoexcept |
Converts a result reference into a result of references.
|
inlineconstexpr |
Swaps two result instances.
If both result instances contain an ok value or both contain an error value, the values are swapped directly. If the two result instances do not contain the same kind of value, the values are moved out of the results temporarily, the old values are destroyed, and new values a move constructed in to the opposite result.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, a copy of the error is returned.
Note that this function is identical to map, but the name transform
makes result able to be a drop in replacement for std::expected
. map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, the error is returned.
Note that this function is identical to map, but the name transform
makes result able to be a drop in replacement for std::expected
. map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, a copy of the error is returned.
Note that this function is identical to map, but the name transform
makes result able to be a drop in replacement for std::expected
. map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the ok value of a result.
If the result contains an ok value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the ok value of a new result, where ok type of the new result is the exact type returned from the callable. If the result contains an error value, the error is returned.
Note that this function is identical to map, but the name transform
makes result able to be a drop in replacement for std::expected
. map
is the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, a copy of the ok value is returned.
Note that this function is identical to map_error, but the name transform_error
makes result able to be a drop in replacement for std::expected
. map_error
would be the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, the ok value is returned.
Note that this function is identical to map_error, but the name transform_error
makes result able to be a drop in replacement for std::expected
. map_error
would be the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, a copy of the ok value is returned.
Note that this function is identical to map_error, but the name transform_error
makes result able to be a drop in replacement for std::expected
. map_error
would be the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Perforams a transformation on the error value of a result.
If the result contains an error value, that value is passed to the transformation callable provided. The value returned from the callable is then returned from this function as the error value of a new result, where the error type of the new result is the exact type returned from the callable. If the result contains an ok value, the ok value is returned.
Note that this function is identical to map_error, but the name transform_error
makes result able to be a drop in replacement for std::expected
. map_error
would be the more typical name of this monadic operation outside of C++.
|
inlineconstexpr |
Converts result<option<T>, E>
into option<result<T, E>>
If the result contains an error, the return value will be a option that contains a result that contains an error. If the result contains an option that contains a value, the return value will be a option that contains result that contains an ok value. If the result contains a option that is none
, the return value will be an option that is none
.
This function only participates in overload resolution of T
is an option.
|
inlineconstexpr |
Converts result<option<T>, E>
into option<result<T, E>>
If the result contains an error, the return value will be a option that contains a result that contains an error. If the result contains an option that contains a value, the return value will be a option that contains result that contains an ok value. If the result contains a option that is none
, the return value will be an option that is none
.
This function only participates in overload resolution of T
is an option.
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Accesses the ok value contained in the result.
This function first checks if the result contains an ok value before attempting to access the value. If the result contains an error, then this function throws an exception.
bad_result_access | Thrown if the result contains an error |
|
inlineconstexpr |
Gets the ok value, or a default if the result is an error.
If the result contains an ok value, that ok value is returned. If the result contains an error value, the provided default value is forwarded, cast, and returned as by the following: static_cast<T>(std::forward<U>(default_value))
.
|
inlineconstexpr |
Gets the ok value, or a default if the result is an error.
If the result contains an ok value, a copy of that ok value is returned. If the result contains an error value, the provided default value is forwarded, cast, and returned as by the following: static_cast<T>(std::forward<U>(default_value))
.
|
inlineconstexpr |
Gets the ok value, or returns the result of a callable.
If the result contains an ok value, a copy of that ok value is returned. If the result contains an error, the provided callable is invoked and the result is cast and returned, as if by: static_cast<T>(std::invoke(std::forward<F>(f)))
.
|
inlineconstexpr |
Gets the ok value, or returns the result of a callable.
If the result contains an ok value, a copy of that ok value is returned. If the result contains an error, the provided callable is invoked and the result is cast and returned, as if by: static_cast<T>(std::invoke(std::forward<F>(f)))
.
|
inlineconstexpr |
Invokes a visitor witht he contained value.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Note that the overload function can be helpful for defining a visiotr inline.
|
inlineconstexpr |
Invokes a visitor witht he contained value.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Note that the overload function can be helpful for defining a visiotr inline.
|
inlineconstexpr |
Invokes a visitor witht he contained value.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Note that the overload function can be helpful for defining a visiotr inline.
|
inlineconstexpr |
Invokes a visitor witht he contained value.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Note that the overload function can be helpful for defining a visiotr inline.
|
inlineconstexpr |
Invokes a visitor with the contained value and meta data.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Unlike visit, this function also passes an extra meta data value when invoking the visitor. This meta data object provides constexpr
information about the type and index of the value being visited. The ok value has index 0 and the error value has index 1. This object has the the API shown below.
Note that the overload function can be helpful for defining a visitor inline.
|
inlineconstexpr |
Invokes a visitor with the contained value and meta data.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Unlike visit, this function also passes an extra meta data value when invoking the visitor. This meta data object provides constexpr
information about the type and index of the value being visited. The ok value has index 0 and the error value has index 1. This object has the the API shown below.
Note that the overload function can be helpful for defining a visitor inline.
|
inlineconstexpr |
Invokes a visitor with the contained value and meta data.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Unlike visit, this function also passes an extra meta data value when invoking the visitor. This meta data object provides constexpr
information about the type and index of the value being visited. The ok value has index 0 and the error value has index 1. This object has the the API shown below.
Note that the overload function can be helpful for defining a visitor inline.
|
inlineconstexpr |
Invokes a visitor with the contained value and meta data.
This function treats a result as if it was a variant of T
and E
(i.e. variant<T, E>
). If the result contians an ok value, that value is passed to the visitor. If the result contains an error, the error is passed to the visitor. When either T
or E
are void
, an instance of void_t is passed instead.
Unlike visit, this function also passes an extra meta data value when invoking the visitor. This meta data object provides constexpr
information about the type and index of the value being visited. The ok value has index 0 and the error value has index 1. This object has the the API shown below.
Note that the overload function can be helpful for defining a visitor inline.
Creates an error valued result, constructed in place.
Similar to STL functions like std::make_unique
and std::make_optional
, this function is provided a template argument to specify what contained error type should be constructed, and then is passed arguments that are forwarded to the constructor of the contained error type.
Creates an error valued result, constructed in place.
Similar to STL functions like std::make_unique
and std::make_optional
, this function is provided a template argument to specify what contained error type should be constructed, and then is passed arguments that are forwarded to the constructor of the contained error type.
|
related |
Gets a result value by index, as if it were a variant.
This function is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
, where index 0 is T
and index 1 is E
.
IDX | The "alternative" index |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by type, as if it were a variant.
This fucntion is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
.
This function only participates in overload resolution if T
and E
are distinct types, in order to avoid ambiguity.
U | The "alternative" type |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by index, as if it were a variant.
This function is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
, where index 0 is T
and index 1 is E
.
IDX | The "alternative" index |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by type, as if it were a variant.
This fucntion is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
.
This function only participates in overload resolution if T
and E
are distinct types, in order to avoid ambiguity.
U | The "alternative" type |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by index, as if it were a variant.
This function is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
, where index 0 is T
and index 1 is E
.
IDX | The "alternative" index |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by type, as if it were a variant.
This fucntion is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
.
This function only participates in overload resolution if T
and E
are distinct types, in order to avoid ambiguity.
U | The "alternative" type |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by index, as if it were a variant.
This function is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
, where index 0 is T
and index 1 is E
.
IDX | The "alternative" index |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
|
related |
Gets a result value by type, as if it were a variant.
This fucntion is provided to make result generically compatible with variant. This function treats result<T, E>
as if it were variant<T, E>
.
This function only participates in overload resolution if T
and E
are distinct types, in order to avoid ambiguity.
U | The "alternative" type |
res | The result to access |
bad_result_access | Thrown if the result does not contain the matching index. |
Creates an ok valued result, constructed in place.
Similar to STL functions like std::make_unique
and std::make_optional
, this function is provided a template argument to specify what contained type should be constructed, and then is passed arguments that are forwarded to the constructor of the contained type.
Creates an ok valued result, constructed in place.
Similar to STL functions like std::make_unique
and std::make_optional
, this function is provided a template argument to specify what contained type should be constructed, and then is passed arguments that are forwarded to the constructor of the contained type.
|
related |
Compares a result with a plain value for equality.
This function return true if the result contains an ok value and the ok value compares equal with the plain value.
|
related |
Compares a result with a plain value for equality.
This function return true if the result contains an ok value and the ok value compares equal with the plain value.
Swaps two result instances.
If both result instances contain an ok value or both contain an error value, the values are swapped directly. If the two result instances do not contain the same kind of value, the values are moved out of the results temporarily, the old values are destroyed, and new values a move constructed in to the opposite result.