result<T>
#
-
template<typename T, typename E = amongoc_status>
class result# Implements a sum type over a success type
T
and an error typeE
. A result \(R\) holds either an instance ofT
or an instance ofE
(reference types are supported!)-
using success_type = T#
-
using error_type = T#
The success type and error type of the result, respectively.
-
result()#
Default-construct to a successful value, if possible.
-
template<typename U>
result(U &&arg)# Conditionally-explicit converting constructor. Requires that
U
is explicit-convertbile toT
but not explicit-convertible toE
. This constructor is explicit unlessU
is implicit-convertbile toT
.
-
template<typename ...Args>
result(success_tag<Args...> tag)# -
template<typename ...Args>
result(error_tag<Args...> tag)# In-place constructs a success value or an error value, respectively.
- Parameters:
tag – The tag contains a tuple of bound constructor arguments that will be forwarded to consrtuct the underlying object.
This requires that the corresponding contained type be constructible from the argument types that are bound within the tag.
-
bool has_value() const#
-
bool has_error() const#
Test whether the result contains a value or an error, respectively.
-
auto error_tag()#
Create an
amongoc::error_tag
object that can be used to construct a newresult
containing the same error as this result.- Precondition:
has_error() == true
This function is cvref-overloaded to perfect-forward the contained error.
-
auto value()#
-
auto operator*()#
Obtain the contained value, or throw an exception if the result is errant.
- Throw:
Throws an error according to
error_traits
This function is cvref-overloaded to perfect-forward the contained value.
-
using success_type = T#
-
template<typename...>
class success_tag# -
template<typename...>
class error_tag# Tag types that are used to construct a
result
with a set of arguments.
-
auto success(auto&&... args)#
-
auto error(auto&&... args)#
Returns a
success_tag
orerror_tag
, respectively, with the given argumounts bound. When the tags are then converted to aresult
, the bound arguments are used to construct the underlying success/error value for thatresult
.Note
The arguments are stored within the tag by-reference (including by r-value reference), so the tag should be immediately used to construct a
result
to avoid dangling references.
-
template<typename E>
struct error_traits# Determines how to throw an exception for a
result
.The following specializations are provided for
E
:std::error_code
Throws a
std::system_error
std::exception_ptr
Calls
std::rethrow_exception
- Any type derived from
std::exception
Simply throws the exception
amongoc_status
Throws an
amongoc::exception
for the status
result
Combinators and Helpers#
-
template<typename F>
class result_fmap# Provides a monadic fmap-style combinator for the given function
F
.-
template<typename T, typename E>
requires std::invocable<F, T>
auto operator()(
) -> result<std::invoke_result_t<F, T>, E># Call the fmap object with a result
res
with a value typeT
and error typeE
.Requires that the wrapped
F
be invocable with aT
and return a new value of type \(R\) (which may be a reference orvoid
).If
res
has a value, then the underlyingF
will be invoked with that value, and the result will be wrapped in a newresult<R, E>
.If
res
does not hold a value, thenF
will not be invoked, and the error inres
will be used to initialize aresult<R, E>
with an errant state.
-
template<typename T, typename E>
-
template<typename T, typename E>
result<T, E> result_join(
)# “Flattens” a result-of-result
r
.Hint
This is an invocable object type with the above signature, not an actual function template.
-
template<typename T, typename E>
requires nanosender<T>
class nanosender_traits<result<T, E>># A specialization of
amongoc::nanosender_traits
that allows a result to act as a nanosender if its success value type is itself a nanosender.