Result
|
The class template result
manages result results from APIs, while encoding possible failure conditions.
More...
#include <result.hpp>
Public Types | |
using | value_type = T |
The value type of this result. | |
using | error_type = E |
The error type of this result. | |
using | failure_type = failure< E > |
The failure type. | |
template<typename U > | |
using | rebind = result< U, E > |
Rebinds the result type. | |
Public Member Functions | |
template<typename U = T, typename = typename std::enable_if<std::is_constructible<U>::value>::type> | |
constexpr | result () noexcept(std::is_nothrow_constructible< U >::value) |
Default-constructs a result with the underlying value type active. More... | |
constexpr | result (const result &other)=default |
Copy constructs this result. More... | |
constexpr | result (result &&other)=default |
Move constructs a result. More... | |
template<typename... Args, typename = typename std::enable_if<std::is_constructible<T,Args...>::value>::type> | |
constexpr | result (in_place_t, Args &&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value) |
Constructs a result object that contains a value. More... | |
template<typename U , typename... Args, typename = typename std::enable_if<std::is_constructible<T, std::initializer_list<U>&, Args...>::value>::type> | |
constexpr | result (in_place_t, std::initializer_list< U > ilist, Args &&...args) noexcept(std::is_nothrow_constructible< T, std::initializer_list< U >, Args... >::value) |
Constructs a result object that contains a value. More... | |
template<typename... Args, typename = typename std::enable_if<std::is_constructible<E,Args...>::value>::type> | |
constexpr | result (in_place_error_t, Args &&... args) noexcept(std::is_nothrow_constructible< E, Args... >::value) |
Constructs a result object that contains an error. More... | |
template<typename U , typename... Args, typename = typename std::enable_if<std::is_constructible<E, std::initializer_list<U>&, Args...>::value>::type> | |
constexpr | result (in_place_error_t, std::initializer_list< U > ilist, Args &&...args) noexcept(std::is_nothrow_constructible< E, std::initializer_list< U >, Args... >::value) |
Constructs a result object that contains an error. More... | |
auto | operator= (const result &other) -> result &=default |
Copy assigns the result stored in other . More... | |
auto | operator= (result &&other) -> result &=default |
Move assigns the result stored in other . More... | |
template<typename T2 , typename E2 , typename = typename std::enable_if<detail::result_is_copy_convert_assignable<T,E,T2,E2>::value>::type> | |
auto | operator= (const result< T2, E2 > &other) noexcept(std::is_nothrow_assignable< T, const T2 & >::value &&std::is_nothrow_assignable< E, const E2 & >::value) -> result & |
Copy-converts the state of other . More... | |
template<typename T2 , typename E2 , typename = typename std::enable_if<detail::result_is_move_convert_assignable<T,E,T2,E2>::value>::type> | |
auto | operator= (result< T2, E2 > &&other) noexcept(std::is_nothrow_assignable< T, T2 && >::value &&std::is_nothrow_assignable< E, E2 && >::value) -> result & |
Move-converts the state of other . More... | |
template<typename U , typename = typename std::enable_if<detail::result_is_value_assignable<T,U>::value>::type> | |
auto | operator= (U &&value) noexcept(std::is_nothrow_assignable< T, U >::value) -> result & |
Perfect-forwarded assignment. More... | |
constexpr RESULT_WARN_UNUSED | operator bool () const noexcept |
Contextually convertible to true if *this contains a value. More... | |
constexpr RESULT_WARN_UNUSED auto | has_value () const noexcept -> bool |
Returns true if *this contains a value. More... | |
constexpr RESULT_WARN_UNUSED auto | has_error () const noexcept -> bool |
Returns true if *this contains an error. More... | |
template<typename U > | |
constexpr RESULT_WARN_UNUSED auto | and_then (U &&value) const -> result< typename std::decay< U >::type, E > |
Returns a result containing value if this result contains a value, otherwise returns a result containing the current error. More... | |
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_implicit_copy_convertible< T, E, T2, E2 >::value, int >::type = 0> | |
result (const result< T2, E2 > &other) noexcept(std::is_nothrow_constructible< T, const T2 & >::value &&std::is_nothrow_constructible< E, const E2 & >::value) | |
Converting copy constructor. More... | |
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_explicit_copy_convertible< T, E, T2, E2 >::value, int >::type = 0> | |
result (const result< T2, E2 > &other) noexcept(std::is_nothrow_constructible< T, const T2 & >::value &&std::is_nothrow_constructible< E, const E2 & >::value) | |
Converting copy constructor. More... | |
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_implicit_move_convertible< T, E, T2, E2 >::value, int >::type = 0> | |
result (result< T2, E2 > &&other) noexcept(std::is_nothrow_constructible< T, T2 && >::value &&std::is_nothrow_constructible< E, E2 && >::value) | |
Converting move constructor. More... | |
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_explicit_move_convertible< T, E, T2, E2 >::value, int >::type = 0> | |
result (result< T2, E2 > &&other) noexcept(std::is_nothrow_constructible< T, T2 && >::value &&std::is_nothrow_constructible< E, E2 && >::value) | |
Converting move constructor. More... | |
template<typename E2 , typename = typename std::enable_if<std::is_constructible<E,const E2&>::value>::type> | |
constexpr | result (const failure< E2 > &e) noexcept(std::is_nothrow_constructible< E, const E2 & >::value) |
Constructs the underlying error of this result. More... | |
template<typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2&&>::value>::type> | |
constexpr | result (failure< E2 > &&e) noexcept(std::is_nothrow_constructible< E, E2 && >::value) |
Constructs the underlying error of this result. More... | |
template<typename U , typename std::enable_if< detail::result_is_explicit_value_convertible< T, U >::value, int >::type = 0> | |
constexpr | result (U &&value) noexcept(std::is_nothrow_constructible< T, U >::value) |
Constructs a result object that contains a value. More... | |
template<typename U , typename std::enable_if< detail::result_is_implicit_value_convertible< T, U >::value, int >::type = 0> | |
constexpr | result (U &&value) noexcept(std::is_nothrow_constructible< T, U >::value) |
Constructs a result object that contains a value. More... | |
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,const E2&>::value>::type> | |
auto | operator= (const failure< E2 > &other) noexcept(std::is_nothrow_assignable< E, const E2 & >::value) -> result & |
Perfect-forwarded assignment. More... | |
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,E2&&>::value>::type> | |
auto | operator= (failure< E2 > &&other) noexcept(std::is_nothrow_assignable< E, E2 && >::value) -> result & |
Perfect-forwarded assignment. More... | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | operator-> () noexcept -> typename std::remove_reference< T >::type * |
Retrieves a pointer to the contained value. More... | |
constexpr RESULT_WARN_UNUSED auto | operator-> () const noexcept -> typename std::remove_reference< typename std::add_const< T >::type >::type * |
Retrieves a pointer to the contained value. More... | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | operator* () &noexcept -> typename std::add_lvalue_reference< T >::type |
Retrieves a reference to the contained value. More... | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | operator* () &&noexcept -> typename std::add_rvalue_reference< T >::type |
Retrieves a reference to the contained value. More... | |
constexpr RESULT_WARN_UNUSED auto | operator* () const &noexcept -> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type |
Retrieves a reference to the contained value. More... | |
constexpr RESULT_WARN_UNUSED auto | operator* () const &&noexcept -> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type |
Retrieves a reference to the contained value. More... | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | value () &-> typename std::add_lvalue_reference< T >::type |
Returns a reference to the contained value. More... | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | value () &&-> typename std::add_rvalue_reference< T >::type |
Returns a reference to the contained value. More... | |
constexpr RESULT_WARN_UNUSED auto | value () const &-> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type |
Returns a reference to the contained value. More... | |
constexpr RESULT_WARN_UNUSED auto | value () const &&-> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type |
Returns a reference to the contained value. More... | |
constexpr RESULT_WARN_UNUSED auto | error () const &noexcept(std::is_nothrow_constructible< E >::value &&std::is_nothrow_copy_constructible< E >::value) -> E |
Returns the contained error, if one exists, or a default-constructed error value. More... | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | error () &&noexcept(std::is_nothrow_constructible< E >::value &&std::is_nothrow_move_constructible< E >::value) -> E |
Returns the contained error, if one exists, or a default-constructed error value. More... | |
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_copy_constructible<E>::value )>::type> | |
RESULT_CPP14_CONSTEXPR auto | expect (String &&message) &-> typename std::add_lvalue_reference< T >::type |
} More... | |
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_move_constructible<E>::value )>::type> | |
RESULT_CPP14_CONSTEXPR auto | expect (String &&message) &&-> typename std::add_rvalue_reference< T >::type |
Returns the contained error, if one exists, or a default-constructed error value. More... | |
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_copy_constructible<E>::value )>::type> | |
RESULT_CPP14_CONSTEXPR auto | expect (String &&message) const &-> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type |
Returns the contained error, if one exists, or a default-constructed error value. More... | |
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_copy_constructible<E>::value )>::type> | |
RESULT_CPP14_CONSTEXPR auto | expect (String &&message) const &&-> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type |
Returns the contained error, if one exists, or a default-constructed error value. More... | |
template<typename U > | |
constexpr RESULT_WARN_UNUSED auto | value_or (U &&default_value) const &-> typename std::remove_reference< T >::type |
Returns the contained value if *this has a value, otherwise returns default_value . More... | |
template<typename U > | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | value_or (U &&default_value) &&-> typename std::remove_reference< T >::type |
Returns the contained value if *this has a value, otherwise returns default_value . More... | |
template<typename U > | |
constexpr RESULT_WARN_UNUSED auto | error_or (U &&default_error) const &-> error_type |
Returns the contained error if *this has an error, otherwise returns default_error . More... | |
template<typename U > | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | error_or (U &&default_error) &&-> error_type |
Returns the contained error if *this has an error, otherwise returns default_error . More... | |
template<typename Fn > | |
constexpr RESULT_WARN_UNUSED auto | flat_map (Fn &&fn) const &-> detail::invoke_result_t< Fn, const T & > |
Invokes the function fn with the value of this result as the argument. More... | |
template<typename Fn > | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | flat_map (Fn &&fn) &&-> detail::invoke_result_t< Fn, T && > |
Invokes the function fn with the value of this result as the argument. More... | |
template<typename Fn > | |
constexpr RESULT_WARN_UNUSED auto | map (Fn &&fn) const &-> result< detail::invoke_result_t< Fn, const T & >, E > |
Invokes the function fn with the value of this result as the argument. More... | |
template<typename Fn > | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | map (Fn &&fn) &&-> result< detail::invoke_result_t< Fn, T && >, E > |
Invokes the function fn with the value of this result as the argument. More... | |
template<typename Fn > | |
constexpr RESULT_WARN_UNUSED auto | map_error (Fn &&fn) const &-> result< T, detail::invoke_result_t< Fn, const E & >> |
Invokes the function fn with the error of this result as the argument. More... | |
template<typename Fn > | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | map_error (Fn &&fn) &&-> result< T, detail::invoke_result_t< Fn, E && >> |
Invokes the function fn with the error of this result as the argument. More... | |
template<typename Fn > | |
constexpr RESULT_WARN_UNUSED auto | flat_map_error (Fn &&fn) const &-> detail::invoke_result_t< Fn, const E & > |
Invokes the function fn with the error of this result as the argument. More... | |
template<typename Fn > | |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto | flat_map_error (Fn &&fn) &&-> detail::invoke_result_t< Fn, E && > |
Invokes the function fn with the error of this result as the argument. More... | |
The class template result
manages result results from APIs, while encoding possible failure conditions.
A common use-case for result is the return value of a function that may fail. As opposed to other approaches, such as std::pair<T,bool>
or std::optional
, result
more accurately conveys the intent of the user along with the failure condition to the caller. This effectively produces an orthogonal error handling mechanism that allows for exception safety while also allowing discrete testability of the return type.
result<T,E>
types may contain a T
value, which signifies that an operation succeeded in producing the result value of type T
. If an result
does not contain a T
value, it will always contain an E
error condition instead.
An result<T,E>
can always be queried for a possible error case by calling the error()
function, even if it contains a value. In the case that a result<T,E>
contains a value object, this will simply return an E
object constructed through default aggregate construction, as if through the expression E{}
, which is assumed to be a "valid" (no-error) state for an E
type. For example:
std::error_code{}
produces a default-construct error-code, which is the "no error" state,0
value (no error), thanks to zero-initialization,std::exception_ptr{}
produces a null-pointer,std::string{}
produces an empty string ""
,When a result<T,E>
contains either a value or error, the storage for that object is guaranteed to be allocated as part of the result object's footprint, i.e. no dynamic memory allocation ever takes place. Thus, a result object models an object, not a pointer, even though the operator*()
and operator->()
are defined.
When an object of type result<T,E>
is contextually converted to bool
, the conversion returns true
if the object contains a value and false
if it contains an error.
result
objects do not have a "valueless" state like variant
s do. Once a result
has been constructed with a value or error, the active underlying type can only be changed through assignment which may is only enabled if construction is guaranteed to be non-throwing. This ensures that a valueless state cannot occur naturally.
Example Use:
fail
can be replaced with failure{...}
thanks to CTAD.T | the underlying value type |
E | the underlying error type |
Definition at line 309 of file result.hpp.
|
constexprnoexcept |
Default-constructs a result with the underlying value type active.
This constructor is only enabled if T
is default-constructible
Basic Usage:
|
constexprdefault |
Copy constructs this result.
If other
contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T
with the expression *other.
If other contains an error, constructs an object that contains a copy of that error.
std::is_copy_constructible<T>::value
or std::is_copy_constructible<E>::value
is false
std::is_trivially_copy_constructible<T>::value
and std::is_trivially_copy_constructible<E>::value
are true
Basic Usage:
other | the result to copy |
|
constexprdefault |
Move constructs a result.
If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other)
and does not make other empty: a moved-from result still contains a value, but the value itself is moved from.
If other contains an error, move-constructs this result from that error.
std::is_move_constructible<T>::value
or std::is_move_constructible<E>::value
is false
std::is_trivially_move_constructible<T>::value
and std::is_trivially_move_constructible<E>::value
are true
Basic Usage:
other | the result to move |
|
noexcept |
Converting copy constructor.
If other
contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T
with the expression *other
.
If other
contains an error, constructs a result object that contains an error, initialized as if direct-initializing (but not direct-list-initializing) an object of type E
.
std::is_constructible_v<T, const U&>
is true
result<T2,E2>
result<T2,E2>
std::is_convertible_v<const T2&, T>
or std::is_convertible_v<const E2&, E>
is false
Basic Usage:
other | the other type to convert |
|
explicitnoexcept |
Converting copy constructor.
If other
contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T
with the expression *other
.
If other
contains an error, constructs a result object that contains an error, initialized as if direct-initializing (but not direct-list-initializing) an object of type E
.
std::is_constructible_v<T, const U&>
is true
result<T2,E2>
result<T2,E2>
std::is_convertible_v<const T2&, T>
or std::is_convertible_v<const E2&, E>
is false
Basic Usage:
other | the other type to convert |
|
noexcept |
Converting move constructor.
If other
contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other)
.
If other
contains an error, constructs a result object that contains an error, initialized as if direct-initializing (but not direct-list-initializing) an object of type E&&.
std::is_constructible_v<T, const U&>
is true
result<T2,E2>
result<T2,E2>
std::is_convertible_v<const T2&, T>
or std::is_convertible_v<const E2&, E>
is false
Basic Usage:
other | the other type to convert |
|
explicitnoexcept |
Converting move constructor.
If other
contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other)
.
If other
contains an error, constructs a result object that contains an error, initialized as if direct-initializing (but not direct-list-initializing) an object of type E&&.
std::is_constructible_v<T, const U&>
is true
result<T2,E2>
result<T2,E2>
std::is_convertible_v<const T2&, T>
or std::is_convertible_v<const E2&, E>
is false
Basic Usage:
other | the other type to convert |
|
explicitconstexprnoexcept |
Constructs a result object that contains a value.
The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T
from the arguments std::forward<Args>(args)...
Basic Usage:
args | the arguments to pass to T's constructor |
|
explicitconstexprnoexcept |
Constructs a result object that contains a value.
The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T
from the arguments std::forward<std::initializer_list<U>>(ilist)
, std::forward<Args>(args)...
Basic Usage:
ilist | An initializer list of entries to forward |
args | the arguments to pass to T's constructor |
|
explicitconstexprnoexcept |
Constructs a result object that contains an error.
the value is initialized as if direct-initializing (but not direct-list-initializing) an object of type E
from the arguments std::forward<Args>(args)...
Basic Usage:
args | the arguments to pass to E's constructor |
|
explicitconstexprnoexcept |
Constructs a result object that contains an error.
The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type E
from the arguments std::forward<std::initializer_list<U>>(ilist)
, std::forward<Args>(args)...
Basic Usage:
ilist | An initializer list of entries to forward |
args | the arguments to pass to Es constructor |
|
constexprnoexcept |
Constructs the underlying error of this result.
E
is constructible from e
Basic Usage:
e | the failure error |
|
constexprnoexcept |
Constructs the underlying error of this result.
E
is constructible from e
Basic Usage:
e | the failure error |
|
explicitconstexprnoexcept |
Constructs a result object that contains a value.
The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value.
std::is_constructible_v<T, U&&>
is true and decay_t<U>
is neither in_place_t
, in_place_error_t
, nor a result
type.std::is_convertible_v<U&&, T>
is false
Basic Usage:
value | the value to copy |
|
constexprnoexcept |
Constructs a result object that contains a value.
The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value.
std::is_constructible_v<T, U&&>
is true and decay_t<U>
is neither in_place_t
, in_place_error_t
, nor a result
type.std::is_convertible_v<U&&, T>
is false
Basic Usage:
value | the value to copy |
|
constexpr |
Returns a result containing value
if this result contains a value, otherwise returns a result containing the current error.
Basic Usage:
value | the value to return as a result |
value
if this contains a value
|
noexcept |
Returns the contained error, if one exists, or a default-constructed error value.
The error()
function will not throw any exceptions if E
does not throw any exceptions for the copy or move construction.
This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.
If this function is invoked on an rvalue of a result, the error is returned via move-construction
std::is_default_constructible<E>::value
is true
std::is_copy_constructible<E>::value
or std::is_move_constructible<E>::value
is true
E{}
represents the "good" (non-error) stateBasic Usage:
|
constexprnoexcept |
Returns the contained error, if one exists, or a default-constructed error value.
The error()
function will not throw any exceptions if E
does not throw any exceptions for the copy or move construction.
This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.
If this function is invoked on an rvalue of a result, the error is returned via move-construction
std::is_default_constructible<E>::value
is true
std::is_copy_constructible<E>::value
or std::is_move_constructible<E>::value
is true
E{}
represents the "good" (non-error) stateBasic Usage:
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::error_or | ( | U && | default_error | ) | && -> error_type |
Returns the contained error if *this
has an error, otherwise returns default_error
.
Basic Usage:
default_error | the error to use in case *this is empty |
default_error
|
constexpr |
Returns the contained error if *this
has an error, otherwise returns default_error
.
Basic Usage:
default_error | the error to use in case *this is empty |
default_error
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::expect | ( | String && | message | ) | && -> typename std::add_rvalue_reference< T >::type |
Returns the contained error, if one exists, or a default-constructed error value.
The error()
function will not throw any exceptions if E
does not throw any exceptions for the copy or move construction.
This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.
If this function is invoked on an rvalue of a result, the error is returned via move-construction
std::is_default_constructible<E>::value
is true
std::is_copy_constructible<E>::value
or std::is_move_constructible<E>::value
is true
E{}
represents the "good" (non-error) stateBasic Usage:
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::expect | ( | String && | message | ) | & -> typename std::add_lvalue_reference< T >::type |
}
Asserts an expectation that this result contains an error, throwing a bad_result_access on failure
If this function is invoked from an rvalue of result
, then this will consume the underlying error first, if there is one.
used
without requiring directly inspecting the underlying value. This is, in effect, equivalent to assert(res.has_value())
, however it uses exceptions to ensure the stack can be unwound, and exceptions invoked.Basic Usage:
message | the message to provide to this expectation |
*this
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::expect | ( | String && | message | ) | const && -> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type |
Returns the contained error, if one exists, or a default-constructed error value.
The error()
function will not throw any exceptions if E
does not throw any exceptions for the copy or move construction.
This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.
If this function is invoked on an rvalue of a result, the error is returned via move-construction
std::is_default_constructible<E>::value
is true
std::is_copy_constructible<E>::value
or std::is_move_constructible<E>::value
is true
E{}
represents the "good" (non-error) stateBasic Usage:
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::expect | ( | String && | message | ) | const & -> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type |
Returns the contained error, if one exists, or a default-constructed error value.
The error()
function will not throw any exceptions if E
does not throw any exceptions for the copy or move construction.
This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.
If this function is invoked on an rvalue of a result, the error is returned via move-construction
std::is_default_constructible<E>::value
is true
std::is_copy_constructible<E>::value
or std::is_move_constructible<E>::value
is true
E{}
represents the "good" (non-error) stateBasic Usage:
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::flat_map | ( | Fn && | fn | ) | && -> detail::invoke_result_t< Fn, T && > |
Invokes the function fn
with the value of this result as the argument.
If this result contains an error, a result of the error is returned
The function being called must return a result
type or the program is ill-formed
If this is called on an rvalue of result
which contains an error, the returned result
is constructed from an rvalue of that error.
Basic Usage:
fn | the function to invoke with this |
|
constexpr |
Invokes the function fn
with the value of this result as the argument.
If this result contains an error, a result of the error is returned
The function being called must return a result
type or the program is ill-formed
If this is called on an rvalue of result
which contains an error, the returned result
is constructed from an rvalue of that error.
Basic Usage:
fn | the function to invoke with this |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::flat_map_error | ( | Fn && | fn | ) | && -> detail::invoke_result_t< Fn, E && > |
Invokes the function fn
with the error of this result as the argument.
If this result contains a value, a result of the value is returned
The function being called must return a result
type or the program is ill-formed
If this is called on an rvalue of result
which contains an error, the returned result
is constructed from an rvalue of that error.
Basic Usage:
fn | the function to invoke with this |
|
constexpr |
Invokes the function fn
with the error of this result as the argument.
If this result contains a value, a result of the value is returned
The function being called must return a result
type or the program is ill-formed
If this is called on an rvalue of result
which contains an error, the returned result
is constructed from an rvalue of that error.
Basic Usage:
fn | the function to invoke with this |
|
constexprnoexcept |
Returns true
if *this
contains an error.
Basic Usage:
true
if *this
contains an error, false
if *this
contains a value
|
constexprnoexcept |
Returns true
if *this
contains a value.
Basic Usage:
true
if *this
contains a value, false
if *this
contains an error RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::map | ( | Fn && | fn | ) | && -> result< detail::invoke_result_t< Fn, T && >, E > |
Invokes the function fn
with the value of this result as the argument.
If this result is an error, the result of this function is that error. Otherwise this function wraps the result and returns it as an result.
If this is called on an rvalue of result
which contains an error, the returned result
is constructed from an rvalue of that error.
Basic Usage:
fn | the function to invoke with this |
|
constexpr |
Invokes the function fn
with the value of this result as the argument.
If this result is an error, the result of this function is that error. Otherwise this function wraps the result and returns it as an result.
If this is called on an rvalue of result
which contains an error, the returned result
is constructed from an rvalue of that error.
Basic Usage:
fn | the function to invoke with this |
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::map_error | ( | Fn && | fn | ) | && -> result< T, detail::invoke_result_t< Fn, E && >> |
Invokes the function fn
with the error of this result as the argument.
If this result contains a value, the result of this function is that value. Otherwise the function is called with that error and returns the result as a result.
If this is called on an rvalue of result
which contains a value, the returned result
is constructed from an rvalue of that value.
Basic Usage:
fn | the function to invoke with this |
|
constexpr |
Invokes the function fn
with the error of this result as the argument.
If this result contains a value, the result of this function is that value. Otherwise the function is called with that error and returns the result as a result.
If this is called on an rvalue of result
which contains a value, the returned result
is constructed from an rvalue of that value.
Basic Usage:
fn | the function to invoke with this |
|
explicitconstexprnoexcept |
Contextually convertible to true
if *this
contains a value.
This function exists to allow for simple, terse checks for containing a value.
Basic Usage:
true
if *this
contains a value, false
if *this
does not contain a value
|
noexcept |
Retrieves a reference to the contained value.
This operator exists to give result
an optional
-like API for cases where it's known that the result
already contains a value.
Care must be taken to ensure that this is only used in safe contexts where a T
value is active.
*this
does not contain a valueBasic Usage:
|
noexcept |
Retrieves a reference to the contained value.
This operator exists to give result
an optional
-like API for cases where it's known that the result
already contains a value.
Care must be taken to ensure that this is only used in safe contexts where a T
value is active.
*this
does not contain a valueBasic Usage:
|
constexprnoexcept |
Retrieves a reference to the contained value.
This operator exists to give result
an optional
-like API for cases where it's known that the result
already contains a value.
Care must be taken to ensure that this is only used in safe contexts where a T
value is active.
*this
does not contain a valueBasic Usage:
|
constexprnoexcept |
Retrieves a reference to the contained value.
This operator exists to give result
an optional
-like API for cases where it's known that the result
already contains a value.
Care must be taken to ensure that this is only used in safe contexts where a T
value is active.
*this
does not contain a valueBasic Usage:
|
constexprnoexcept |
Retrieves a pointer to the contained value.
This operator exists to give result
an optional
-like API for cases where it's known that the result
already contains a value.
Care must be taken to ensure that this is only used in safe contexts where a T
value is active.
*this
does not contain a value.Basic Usage:
|
noexcept |
Retrieves a pointer to the contained value.
This operator exists to give result
an optional
-like API for cases where it's known that the result
already contains a value.
Care must be taken to ensure that this is only used in safe contexts where a T
value is active.
*this
does not contain a value.Basic Usage:
|
noexcept |
Perfect-forwarded assignment.
Depending on whether *this
contains an error before the call, the contained error is either direct-initialized via forwarding the error, or assigned from forwarding the error
std::is_nothrow_constructible_v<E, E2>
is true
, andstd::is_assignable_v<E&, E2>
is true
other | the failure value to assign to this |
(*this)
|
default |
Copy assigns the result stored in other
.
std::is_nothrow_copy_constructible_v<T>
is true
, andstd::is_nothrow_copy_constructible_v<E>
is true
this restriction guarantees that no 'true
:std::is_trivially_copy_constructible<T>::value
std::is_trivially_copy_constructible<E>::value
std::is_trivially_copy_assignable<T>::value
std::is_trivially_copy_assignable<E>::value
std::is_trivially_destructible<T>::value
std::is_trivially_destructible<E>::value
other | the other result to copy |
|
noexcept |
Copy-converts the state of other
.
If both *this
and other
contain either values or errors, the underlying value is constructed as if through assignment.
Otherwise if *this
contains a value, but other
contains an error, then the contained value is destroyed by calling its destructor. *this
will no longer contain a value after the call, and will now contain E
constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of const E2&
.
If other
contains a value and *this
contains an error, then the contained error is destroyed by calling its destructor. *this
now contains a value constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of const T2&
.
std::is_nothrow_constructible_v<T, const T2&>
, std::is_assignable_v<T&, const T2&>
, std::is_nothrow_constructible_v<E, const E2&>
, std::is_assignable_v<E&, const E2&>
are all true.result<T2,E2>
other | the other result object to convert |
(*this)
|
noexcept |
Perfect-forwarded assignment.
Depending on whether *this
contains an error before the call, the contained error is either direct-initialized via forwarding the error, or assigned from forwarding the error
std::is_nothrow_constructible_v<E, E2>
is true
, andstd::is_assignable_v<E&, E2>
is true
other | the failure value to assign to this |
(*this)
|
default |
Move assigns the result stored in other
.
std::is_nothrow_copy_constructible_v<T>
is true
, andstd::is_nothrow_copy_constructible_v<E>
is true
this restriction guarantees that no 'valueless_by_exception` state may occur.true
:std::is_trivially_move_constructible<T>::value
std::is_trivially_move_constructible<E>::value
std::is_trivially_move_assignable<T>::value
std::is_trivially_move_assignable<E>::value
std::is_trivially_destructible<T>::value
std::is_trivially_destructible<E>::value
other | the other result to copy |
|
noexcept |
Move-converts the state of other
.
If both *this
and other
contain either values or errors, the underlying value is constructed as if through move-assignment.
Otherwise if *this
contains a value, but other
contains an error, then the contained value is destroyed by calling its destructor. *this
will no longer contain a value after the call, and will now contain E
constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of E2&&
.
If other
contains a value and *this
contains an error, then the contained error is destroyed by calling its destructor. *this
now contains a value constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of T2&&
.
std::is_nothrow_constructible_v<T, T2&&>
, std::is_assignable_v<T&, T2&&>
, std::is_nothrow_constructible_v<E, E2&&>
, std::is_assignable_v<E&, E2&&>
are all true.result<T2,E2>
other | the other result object to convert |
(*this)
|
noexcept |
Perfect-forwarded assignment.
Depending on whether *this
contains a value before the call, the contained value is either direct-initialized from std::forward<U>(value) or assigned from std::forward<U>(value).
std::decay_t<U>
is not a result type,std::decay_t<U>
is not a failure typestd::is_nothrow_constructible_v<T, U>
is true
std::is_assignable_v<T&, U>
is true
true
:T
is not a scalar type;std::decay_t<U>
is not T
.value | to assign to the contained value |
(*this)
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::value | ( | ) | && -> typename std::add_rvalue_reference< T >::type |
Returns a reference to the contained value.
This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.
If this contains an error, an exception is thrown containing the underlying error. The error is consumed propagating the same constness and refness of this result.
Basic Usage:
bad_result_access<E> | if *this does not contain a value. |
*this
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::value | ( | ) | & -> typename std::add_lvalue_reference< T >::type |
Returns a reference to the contained value.
This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.
If this contains an error, an exception is thrown containing the underlying error. The error is consumed propagating the same constness and refness of this result.
Basic Usage:
bad_result_access<E> | if *this does not contain a value. |
*this
|
constexpr |
Returns a reference to the contained value.
This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.
If this contains an error, an exception is thrown containing the underlying error. The error is consumed propagating the same constness and refness of this result.
Basic Usage:
bad_result_access<E> | if *this does not contain a value. |
*this
|
constexpr |
Returns a reference to the contained value.
This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.
If this contains an error, an exception is thrown containing the underlying error. The error is consumed propagating the same constness and refness of this result.
Basic Usage:
bad_result_access<E> | if *this does not contain a value. |
*this
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::value_or | ( | U && | default_value | ) | && -> typename std::remove_reference< T >::type |
Returns the contained value if *this
has a value, otherwise returns default_value
.
Basic Usage:
default_value | the value to use in case *this contains an error |
default_value
|
constexpr |
Returns the contained value if *this
has a value, otherwise returns default_value
.
Basic Usage:
default_value | the value to use in case *this contains an error |
default_value