Although this library is heavily based on the std::expected proposal, it's
important to be aware that this handles a few things differently from what is
described in P0323. These deviations are outline below
-
Most obviously, this type is named
result, rather thanexpected, sinceexpected<T,E>'s name conveys thatEis an expected result, when in actuality it is unexpected. Theresult<T,E>is more idiomatic in other modern languages like Swift and Rust, and carries no such implication. To account for the name change,unexpected<E>is also namedfailure<E>, and a helper factory functionfailexists better code readability. -
This does not implement
emplacefunctions, since this functionality is seen as orthogonal to the goals ofexpected, which should not be to arbitrarily be aneither<T,E>type -
expected(result) has been given support for referenceTtypes, so that this may behave as a consistent vocabulary type for error-handling -
unexpected(failure) has been given support for referenceEtypes, in order to avoid expensive construction of objects only used for comparisons -
Assignment operators are only enabled if the corresponding constructors are marked
noexcept. This deviates fromstd::expected's proposal of introducing an intermediate object to hold the type during assignment. -
Rather than allowing direct referential access to the underlying error,
result::error()always returns a value that acts as the result's status. The defaut-constructed state ofEis always considered the "good" state (e.g.std::error_code{},std::errc{}, etc). This reduces the number of cases where this API may throw an exception to justvalue() -
Rather than using
unexpect_tto denote in-place construction of errors, this library usesin_place_error_t. This change was necessary with the rename ofexpected->result.