|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Minimal std::expected-style result type for C++20.
More...
#include <ah-cpp-compat.H>
Public Types | |
| using | value_type = T |
| using | error_type = E |
| using | unexpected_type = unexpected< E > |
Public Member Functions | |
| constexpr | expected () |
| Default-construct with a value. | |
| template<class U = T, class = std::enable_if_t< std::is_constructible_v<T, U &&> and not std::is_same_v<cpp_compat_detail::remove_cvref_t<U>, expected> and not std::is_same_v<cpp_compat_detail::remove_cvref_t<U>, unexpect_t>>> | |
| constexpr | expected (U &&v) |
| Construct from a value. | |
| template<class G > | |
| constexpr | expected (const unexpected< G > &u) |
Construct an error from unexpected (lvalue). | |
| template<class G > | |
| constexpr | expected (unexpected< G > &&u) |
Construct an error from unexpected (rvalue). | |
| template<class... Args> | |
| constexpr | expected (unexpect_t, Args &&...args) |
| Construct an error in place. | |
| expected (const expected &o) | |
| Copy constructor. | |
| expected (expected &&o) noexcept(std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_constructible_v< E >) | |
| Move constructor. | |
| expected & | operator= (const expected &o) |
| Copy assignment. | |
| expected & | operator= (expected &&o) noexcept(std::is_nothrow_move_constructible_v< T > and std::is_nothrow_move_constructible_v< E >) |
| Move assignment. | |
| ~expected () | |
| Destructor. | |
| constexpr bool | has_value () const noexcept |
| Check if a value is held. | |
| constexpr | operator bool () const noexcept |
| Contextual conversion to bool. | |
| constexpr const T & | operator* () const &noexcept |
| Access the value (const lvalue). | |
| constexpr T & | operator* () &noexcept |
| Access the value (lvalue). | |
| constexpr const T * | operator-> () const noexcept |
| Access the value via pointer (const). | |
| constexpr T * | operator-> () noexcept |
| Access the value via pointer (mutable). | |
| constexpr const T & | value () const & |
| Access the value with exception on error (const lvalue). | |
| constexpr T & | value () & |
| Access the value with exception on error (lvalue). | |
| constexpr const E & | error () const &noexcept |
| Access the error (const lvalue). | |
| constexpr E & | error () &noexcept |
| Access the error (lvalue). | |
| template<class U > | |
| constexpr T | value_or (U &&default_value) const & |
| Get the value or a default. | |
| template<class F > | |
| auto | and_then (F &&f) const & |
| Monadic bind: chain another fallible operation. | |
| template<class F > | |
| auto | transform (F &&f) const & |
| Monadic map: transform the value. | |
| template<class F > | |
| auto | or_else (F &&f) const & |
| Monadic error recovery: handle the error. | |
Private Member Functions | |
| void | destroy () noexcept |
Private Attributes | ||
| union { | ||
| T val_ | ||
| E err_ | ||
| }; | ||
| bool | has_ | |
Minimal std::expected-style result type for C++20.
Holds either a value of type T or an error of type E. Aliased to std::expected when the toolchain provides <expected>. See the file header for the supported surface.
| T | value type. |
| E | error type. |
Definition at line 285 of file ah-cpp-compat.H.
Definition at line 307 of file ah-cpp-compat.H.
| using Aleph::expected< T, E >::unexpected_type = unexpected<E> |
Definition at line 308 of file ah-cpp-compat.H.
Definition at line 306 of file ah-cpp-compat.H.
Default-construct with a value.
Constructs the value member via default-initialization.
T must be default-constructible. has_value() == true. | Any | exception thrown by T's default constructor. |
T construction.Definition at line 320 of file ah-cpp-compat.H.
|
inlineconstexpr |
Construct from a value.
Implicit conversion from U to expected<T,E> when T is constructible from U. Enables patterns like return 42; from a function returning expected<int, Error>.
| U | Value type (forwarded to T). |
| v | The value to store. |
has_value() == true and *this == v. | Any | exception thrown by T(std::forward<U>(v)). |
T construction.Definition at line 340 of file ah-cpp-compat.H.
|
inlineconstexpr |
Construct an error from unexpected (lvalue).
| G | Error type of the unexpected (must convert to E). |
| u | The unexpected error wrapper. |
has_value() == false and error() == u.error(). | Any | exception thrown by E(u.error()). |
E construction.Definition at line 353 of file ah-cpp-compat.H.
|
inlineconstexpr |
Construct an error from unexpected (rvalue).
| G | Error type of the unexpected (must convert to E). |
| u | The unexpected error wrapper. |
has_value() == false and error() == std::move(u).error(). | Any | exception thrown by E(std::move(u).error()). |
E construction.Definition at line 366 of file ah-cpp-compat.H.
|
inlineexplicitconstexpr |
Construct an error in place.
Pass Aleph::unexpect as the first argument to construct the error from arbitrary arguments forwarded to E's constructor:
| Args | Argument types forwarded to E. |
| args | Arguments forwarded to E's constructor. |
has_value() == false. | Any | exception thrown by E(std::forward<Args>(args)...). |
E construction.Definition at line 386 of file ah-cpp-compat.H.
Copy constructor.
Copies the active member (value or error) from o.
| o | Source expected to copy from. |
has_value() == o.has_value() and active member equals o's. | Any | exception thrown by T or E copy constructor. |
Definition at line 401 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
|
inlinenoexcept |
Move constructor.
Moves the active member (value or error) from o.
| o | Source expected to move from. |
has_value() == o.has_value() and active member moved from o. T and EDefinition at line 419 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
Destructor.
Destroys the active member (value or error).
Definition at line 493 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::destroy().
|
inline |
Monadic bind: chain another fallible operation.
If a value is held, invokes f(value) and returns its result (which must be an expected). If an error is held, propagates the error without calling f. Enables chaining fallible operations:
| F | Callable type: F(const T&) -> expected<U, E>. |
| f | Function to apply to the value. |
f(value) if value is held, otherwise expected<U,E>(error). f invocation if value is held.Definition at line 621 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
Definition at line 297 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
Referenced by Aleph::expected< T, E >::~expected(), Aleph::expected< void, E >::~expected(), Aleph::expected< T, E >::operator=(), Aleph::expected< void, E >::operator=(), Aleph::expected< void, E >::operator=(), and Aleph::expected< T, E >::operator=().
|
inlineconstexprnoexcept |
Access the error (lvalue).
has_value() == false. Behavior is undefined if a value is held. Definition at line 582 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::err_.
|
inlineconstexprnoexcept |
Access the error (const lvalue).
has_value() == false. Behavior is undefined if a value is held. Definition at line 575 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::err_.
|
inlineconstexprnoexcept |
Check if a value is held.
true if the object holds a value, false if it holds an error. Definition at line 501 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::has_.
|
inlineexplicitconstexprnoexcept |
Contextual conversion to bool.
true if the object holds a value, false if it holds an error. if (e) ... where e is an expected.Definition at line 508 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::has_.
|
inlineconstexprnoexcept |
Access the value (lvalue).
has_value() == true. Behavior is undefined if an error is held. Definition at line 522 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::val_.
|
inlineconstexprnoexcept |
Access the value (const lvalue).
has_value() == true. Behavior is undefined if an error is held. Definition at line 515 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::val_.
|
inlineconstexprnoexcept |
Access the value via pointer (const).
has_value() == true. Behavior is undefined if an error is held. Definition at line 529 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::val_.
|
inlineconstexprnoexcept |
Access the value via pointer (mutable).
has_value() == true. Behavior is undefined if an error is held. Definition at line 539 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::val_.
|
inline |
Copy assignment.
Replaces the active member with a copy of o's active member. Uses a copy-and-move idiom to provide strong exception safety: if construction of the new member throws, *this is left in its original state.
| o | Source expected to assign from. |
*this. has_value() == o.has_value() and active member equals o's. | Any | exception thrown by T or E copy/move constructor. |
*this unchanged.Definition at line 442 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::destroy(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
|
inlinenoexcept |
Move assignment.
Replaces the active member by moving from o. Uses move-and-swap idiom to provide strong exception safety.
| o | Source expected to move from. |
*this. has_value() == o.has_value() and active member moved from o. T and E have noexcept move ctors.Definition at line 469 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::destroy(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
|
inline |
Monadic error recovery: handle the error.
If an error is held, invokes f(error) and returns its result (which must be an expected). If a value is held, propagates the value without calling f. Use this to recover from errors or provide fallbacks:
| F | Callable type: F(const E&) -> expected<T, F>. |
| f | Function to apply to the error. |
f(error) if error is held, otherwise expected<T,F>(value). f invocation if error is held.Definition at line 672 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
|
inline |
Monadic map: transform the value.
If a value is held, invokes f(value) and wraps the result in an expected<U, E>. If an error is held, propagates the error. Use this to apply pure transformations without changing the error channel:
| F | Callable type: F(const T&) -> U. |
| f | Function to apply to the value. |
expected<U,E>(f(value)) if value is held, otherwise expected<U,E>(error). f invocation if value is held.Definition at line 644 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
Referenced by TEST().
Access the value with exception on error (lvalue).
| bad_expected_access<E> | if an error is held. |
Definition at line 563 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
|
inlineconstexpr |
Access the value with exception on error (const lvalue).
| bad_expected_access<E> | if an error is held. |
Definition at line 550 of file ah-cpp-compat.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::expected< T, E >::err_, Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
|
inlineconstexpr |
Get the value or a default.
Returns the stored value if present, otherwise returns default_value converted to T. Useful for providing fallbacks without branching:
| U | Type of the default value (must convert to T). |
| default_value | Fallback value to return if an error is held. |
static_cast<T>(default_value). T copy if value is held, or T construction from U if not.Definition at line 598 of file ah-cpp-compat.H.
References Aleph::expected< T, E >::has_, and Aleph::expected< T, E >::val_.
| union { ... } Aleph::expected< T, E > |
| E Aleph::expected< T, E >::err_ |
Definition at line 293 of file ah-cpp-compat.H.
Referenced by Aleph::expected< T, E >::expected(), Aleph::expected< void, E >::expected(), Aleph::expected< void, E >::expected(), Aleph::expected< T, E >::expected(), Aleph::expected< void, E >::and_then(), Aleph::expected< T, E >::and_then(), Aleph::expected< T, E >::destroy(), Aleph::expected< void, E >::destroy(), Aleph::expected< T, E >::error(), Aleph::expected< void, E >::error(), Aleph::expected< T, E >::error(), Aleph::expected< void, E >::error(), Aleph::expected< T, E >::operator=(), Aleph::expected< void, E >::operator=(), Aleph::expected< void, E >::operator=(), Aleph::expected< T, E >::operator=(), Aleph::expected< void, E >::or_else(), Aleph::expected< T, E >::or_else(), Aleph::expected< T, E >::transform(), Aleph::expected< T, E >::value(), Aleph::expected< void, E >::value(), and Aleph::expected< T, E >::value().
Definition at line 295 of file ah-cpp-compat.H.
Referenced by Aleph::expected< T, E >::expected(), Aleph::expected< void, E >::expected(), Aleph::expected< void, E >::expected(), Aleph::expected< T, E >::expected(), Aleph::expected< void, E >::and_then(), Aleph::expected< T, E >::and_then(), Aleph::expected< T, E >::destroy(), Aleph::expected< void, E >::destroy(), Aleph::expected< T, E >::has_value(), Aleph::expected< void, E >::has_value(), Aleph::expected< T, E >::operator bool(), Aleph::expected< void, E >::operator bool(), Aleph::expected< T, E >::operator=(), Aleph::expected< void, E >::operator=(), Aleph::expected< void, E >::operator=(), Aleph::expected< T, E >::operator=(), Aleph::expected< void, E >::or_else(), Aleph::expected< T, E >::or_else(), Aleph::expected< T, E >::transform(), Aleph::expected< T, E >::value(), Aleph::expected< void, E >::value(), Aleph::expected< T, E >::value(), and Aleph::expected< T, E >::value_or().
Definition at line 292 of file ah-cpp-compat.H.
Referenced by Aleph::expected< T, E >::expected(), Aleph::expected< T, E >::expected(), Aleph::expected< T, E >::and_then(), Aleph::expected< T, E >::destroy(), Aleph::expected< T, E >::operator*(), Aleph::expected< T, E >::operator*(), Aleph::expected< T, E >::operator->(), Aleph::expected< T, E >::operator->(), Aleph::expected< T, E >::operator=(), Aleph::expected< T, E >::operator=(), Aleph::expected< T, E >::or_else(), Aleph::expected< T, E >::transform(), Aleph::expected< T, E >::value(), Aleph::expected< T, E >::value(), and Aleph::expected< T, E >::value_or().