Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::expected< T, E > Class Template Reference

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_
 

Detailed Description

template<class T, class E>
class Aleph::expected< T, E >

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.

Template Parameters
Tvalue type.
Eerror type.

Definition at line 285 of file ah-cpp-compat.H.

Member Typedef Documentation

◆ error_type

template<class T , class E >
using Aleph::expected< T, E >::error_type = E

Definition at line 307 of file ah-cpp-compat.H.

◆ unexpected_type

template<class T , class E >
using Aleph::expected< T, E >::unexpected_type = unexpected<E>

Definition at line 308 of file ah-cpp-compat.H.

◆ value_type

template<class T , class E >
using Aleph::expected< T, E >::value_type = T

Definition at line 306 of file ah-cpp-compat.H.

Constructor & Destructor Documentation

◆ expected() [1/7]

template<class T , class E >
constexpr Aleph::expected< T, E >::expected ( )
inlineconstexpr

Default-construct with a value.

Constructs the value member via default-initialization.

Precondition
T must be default-constructible.
Postcondition
has_value() == true.
Exceptions
Anyexception thrown by T's default constructor.
Complexity O(1) plus T construction.
Exception Safety Basic guarantee: if construction throws, no object.

Definition at line 320 of file ah-cpp-compat.H.

◆ expected() [2/7]

template<class T , class E >
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 Aleph::expected< T, E >::expected ( U &&  v)
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>.

Template Parameters
UValue type (forwarded to T).
Parameters
vThe value to store.
Postcondition
has_value() == true and *this == v.
Exceptions
Anyexception thrown by T(std::forward<U>(v)).
Complexity O(1) plus T construction.
Exception Safety Basic guarantee.

Definition at line 340 of file ah-cpp-compat.H.

◆ expected() [3/7]

template<class T , class E >
template<class G >
constexpr Aleph::expected< T, E >::expected ( const unexpected< G > &  u)
inlineconstexpr

Construct an error from unexpected (lvalue).

Template Parameters
GError type of the unexpected (must convert to E).
Parameters
uThe unexpected error wrapper.
Postcondition
has_value() == false and error() == u.error().
Exceptions
Anyexception thrown by E(u.error()).
Complexity O(1) plus E construction.

Definition at line 353 of file ah-cpp-compat.H.

◆ expected() [4/7]

template<class T , class E >
template<class G >
constexpr Aleph::expected< T, E >::expected ( unexpected< G > &&  u)
inlineconstexpr

Construct an error from unexpected (rvalue).

Template Parameters
GError type of the unexpected (must convert to E).
Parameters
uThe unexpected error wrapper.
Postcondition
has_value() == false and error() == std::move(u).error().
Exceptions
Anyexception thrown by E(std::move(u).error()).
Complexity O(1) plus E construction.

Definition at line 366 of file ah-cpp-compat.H.

◆ expected() [5/7]

template<class T , class E >
template<class... Args>
constexpr Aleph::expected< T, E >::expected ( unexpect_t  ,
Args &&...  args 
)
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:

expected<int, std::string> e(unexpect, 5, 'x'); // E constructed as string(5,'x')
Minimal std::expected-style result type for C++20.
constexpr unexpect_t unexpect
Tag value of type unexpect_t.
Template Parameters
ArgsArgument types forwarded to E.
Parameters
argsArguments forwarded to E's constructor.
Postcondition
has_value() == false.
Exceptions
Anyexception thrown by E(std::forward<Args>(args)...).
Complexity O(1) plus E construction.

Definition at line 386 of file ah-cpp-compat.H.

◆ expected() [6/7]

template<class T , class E >
Aleph::expected< T, E >::expected ( const expected< T, E > &  o)
inline

Copy constructor.

Copies the active member (value or error) from o.

Parameters
oSource expected to copy from.
Postcondition
has_value() == o.has_value() and active member equals o's.
Exceptions
Anyexception thrown by T or E copy constructor.
Complexity O(1) plus active member copy.
Exception Safety Basic guarantee.

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_.

◆ expected() [7/7]

template<class T , class E >
Aleph::expected< T, E >::expected ( expected< T, E > &&  o)
inlinenoexcept

Move constructor.

Moves the active member (value or error) from o.

Parameters
oSource expected to move from.
Postcondition
has_value() == o.has_value() and active member moved from o.
Complexity O(1) plus active member move.
Exception Safety Conditional noexcept: noexcept if both T and E
have noexcept move constructors, otherwise basic guarantee.

Definition 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_.

◆ ~expected()

template<class T , class E >
Aleph::expected< T, E >::~expected ( )
inline

Destructor.

Destroys the active member (value or error).

Complexity O(1) plus active member destruction.
Exception Safety No-throw guarantee.

Definition at line 493 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::destroy().

Member Function Documentation

◆ and_then()

template<class T , class E >
template<class F >
auto Aleph::expected< T, E >::and_then ( F &&  f) const &
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:

auto res = parse(input)
.and_then([](int v) -> expected<int, Error> { return validate(v); })
.and_then([](int v) -> expected<int, Error> { return process(v); });\n
auto and_then(F &&f) const &
Monadic bind: chain another fallible operation.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Template Parameters
FCallable type: F(const T&) -> expected<U, E>.
Parameters
fFunction to apply to the value.
Returns
Result of f(value) if value is held, otherwise expected<U,E>(error).
Complexity O(1) plus 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_.

Referenced by TEST(), and TEST().

◆ destroy()

◆ error() [1/2]

template<class T , class E >
constexpr E & Aleph::expected< T, E >::error ( ) &
inlineconstexprnoexcept

Access the error (lvalue).

Returns
Mutable reference to the stored error.
Precondition
has_value() == false. Behavior is undefined if a value is held.
Complexity O(1).

Definition at line 582 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::err_.

◆ error() [2/2]

template<class T , class E >
constexpr const E & Aleph::expected< T, E >::error ( ) const &
inlineconstexprnoexcept

Access the error (const lvalue).

Returns
Const reference to the stored error.
Precondition
has_value() == false. Behavior is undefined if a value is held.
Complexity O(1).

Definition at line 575 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::err_.

Referenced by TEST(), and TEST().

◆ has_value()

template<class T , class E >
constexpr bool Aleph::expected< T, E >::has_value ( ) const
inlineconstexprnoexcept

Check if a value is held.

Returns
true if the object holds a value, false if it holds an error.
Complexity O(1).

Definition at line 501 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::has_.

Referenced by TEST(), TEST(), TEST(), and TEST().

◆ operator bool()

template<class T , class E >
constexpr Aleph::expected< T, E >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Contextual conversion to bool.

Returns
true if the object holds a value, false if it holds an error.
Complexity O(1).
Usage Enables if (e) ... where e is an expected.

Definition at line 508 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::has_.

◆ operator*() [1/2]

template<class T , class E >
constexpr T & Aleph::expected< T, E >::operator* ( ) &
inlineconstexprnoexcept

Access the value (lvalue).

Returns
Mutable reference to the stored value.
Precondition
has_value() == true. Behavior is undefined if an error is held.
Complexity O(1).

Definition at line 522 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::val_.

◆ operator*() [2/2]

template<class T , class E >
constexpr const T & Aleph::expected< T, E >::operator* ( ) const &
inlineconstexprnoexcept

Access the value (const lvalue).

Returns
Const reference to the stored value.
Precondition
has_value() == true. Behavior is undefined if an error is held.
Complexity O(1).

Definition at line 515 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::val_.

◆ operator->() [1/2]

template<class T , class E >
constexpr const T * Aleph::expected< T, E >::operator-> ( ) const
inlineconstexprnoexcept

Access the value via pointer (const).

Returns
Const pointer to the stored value.
Precondition
has_value() == true. Behavior is undefined if an error is held.
Complexity O(1).

Definition at line 529 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::val_.

◆ operator->() [2/2]

template<class T , class E >
constexpr T * Aleph::expected< T, E >::operator-> ( )
inlineconstexprnoexcept

Access the value via pointer (mutable).

Returns
Mutable pointer to the stored value.
Precondition
has_value() == true. Behavior is undefined if an error is held.
Complexity O(1).

Definition at line 539 of file ah-cpp-compat.H.

References Aleph::expected< T, E >::val_.

◆ operator=() [1/2]

template<class T , class E >
expected & Aleph::expected< T, E >::operator= ( const expected< T, E > &  o)
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.

Parameters
oSource expected to assign from.
Returns
*this.
Postcondition
has_value() == o.has_value() and active member equals o's.
Exceptions
Anyexception thrown by T or E copy/move constructor.
Complexity O(1) plus destruction + construction.
Exception Safety Strong guarantee: if copy fails, *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_.

◆ operator=() [2/2]

template<class T , class E >
expected & Aleph::expected< T, E >::operator= ( expected< T, E > &&  o)
inlinenoexcept

Move assignment.

Replaces the active member by moving from o. Uses move-and-swap idiom to provide strong exception safety.

Parameters
oSource expected to move from.
Returns
*this.
Postcondition
has_value() == o.has_value() and active member moved from o.
Complexity O(1) plus destruction + move construction.
Exception Safety Strong guarantee if move is noexcept; otherwise basic.
Conditional noexcept if both 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_.

◆ or_else()

template<class T , class E >
template<class F >
auto Aleph::expected< T, E >::or_else ( F &&  f) const &
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:

auto recovered = parse(input)
.or_else([](const Error &e) -> expected<int, Error> {
log(e);
return 0; // default value on error
});
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4074
Template Parameters
FCallable type: F(const E&) -> expected<T, F>.
Parameters
fFunction to apply to the error.
Returns
Result of f(error) if error is held, otherwise expected<T,F>(value).
Complexity O(1) plus 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_.

Referenced by TEST(), and TEST().

◆ transform()

template<class T , class E >
template<class F >
auto Aleph::expected< T, E >::transform ( F &&  f) const &
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:

auto doubled = parse(input).transform([](int v) { return v * 2; });
Template Parameters
FCallable type: F(const T&) -> U.
Parameters
fFunction to apply to the value.
Returns
expected<U,E>(f(value)) if value is held, otherwise expected<U,E>(error).
Complexity O(1) plus 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().

◆ value() [1/2]

template<class T , class E >
constexpr T & Aleph::expected< T, E >::value ( ) &
inlineconstexpr

Access the value with exception on error (lvalue).

Returns
Mutable reference to the stored value.
Exceptions
bad_expected_access<E>if an error is held.
Complexity O(1).
Exception Safety Strong guarantee: throws if 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_.

◆ value() [2/2]

template<class T , class E >
constexpr const T & Aleph::expected< T, E >::value ( ) const &
inlineconstexpr

Access the value with exception on error (const lvalue).

Returns
Const reference to the stored value.
Exceptions
bad_expected_access<E>if an error is held.
Complexity O(1).
Exception Safety Strong guarantee: throws if 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_.

Referenced by TEST(), TEST(), and TEST().

◆ value_or()

template<class T , class E >
template<class U >
constexpr T Aleph::expected< T, E >::value_or ( U &&  default_value) const &
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:

int x = parse(input).value_or(0); // 0 if parse failed
Template Parameters
UType of the default value (must convert to T).
Parameters
default_valueFallback value to return if an error is held.
Returns
The stored value if present, otherwise static_cast<T>(default_value).
Complexity O(1) plus 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_.

Referenced by TEST(), and TEST().

Member Data Documentation

◆ [union]

union { ... } Aleph::expected< T, E >

◆ err_

◆ has_

◆ val_


The documentation for this class was generated from the following file: