tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
Expected< T, E > Class Template Reference

A discriminated union holding either a success value or an error. More...

#include <expected.h>

Public Types

using value_type = T
 
using error_type = E
 

Public Member Functions

 Expected (T &&v) noexcept(is_nothrow_move_constructible_v< T >)
 Construct in the success state by moving a value in.
 
 Expected (Unexpected< E > u) noexcept
 Construct in the error state from an Unexpected tag.
 
 Expected (Expected &&other) noexcept(is_nothrow_move_constructible_v< T >)
 Move constructor.
 
Expectedoperator= (Expected &&other) noexcept(is_nothrow_move_constructible_v< T >)
 Move assignment.
 
 Expected (const Expected &)=delete
 Deleted — Expected is move-only to prevent accidental copies.
 
Expectedoperator= (const Expected &)=delete
 Deleted — Expected is move-only to prevent accidental copies.
 
 ~Expected () noexcept
 Destructor. Destroys the active union member.
 
 operator bool () const noexcept
 Returns true if the Expected holds a success value.
 
bool has_value () const noexcept
 Returns true if the Expected holds a success value.
 
T & value () noexcept
 Access the success value by reference.
 
const T & value () const noexcept
 Access the success value by const reference.
 
T & operator* () noexcept
 Dereference operator — access the success value.
 
const T & operator* () const noexcept
 Dereference operator (const) — access the success value.
 
T * operator-> () noexcept
 Arrow operator — access members of the success value.
 
const T * operator-> () const noexcept
 Arrow operator (const) — access members of the success value.
 
error () const noexcept
 Access the error code.
 

Detailed Description

template<typename T, typename E>
class Expected< T, E >

A discriminated union holding either a success value or an error.

Designed for failable operations in the fused library (decompositions, solvers, etc.). Uses a union internally so that only the active member is ever constructed — on the error path, T is never touched.

Move-only by design to prevent accidental copies of large results (e.g. matrix types) on embedded targets. With C++17 guaranteed copy elision and NRVO, returning by value from algorithms is zero-cost.

Marked [[nodiscard]] so the compiler warns if a caller discards the result without checking the error state.

Template Parameters
TSuccess value type.
EError type (e.g. MatrixStatus, FilterStatus). No default — callers must always specify the error type explicitly.
Example — returning success:
Expected<Matrix3f, MatrixStatus> cholesky(const Matrix3f& A) {
Matrix3f L;
// ... compute L ...
return move(L); // or just `return L;` with NRVO
}
A discriminated union holding either a success value or an error.
Definition expected.h:86
constexpr remove_reference_t< T > && move(T &&t) noexcept
Cast to rvalue reference (replacement for std::move).
Definition simple_type_traits.h:178
Example — returning an error:
if (!A.isSymmetric())
return Unexpected{MatrixStatus::NotSymmetric};
Tag type for constructing an Expected in the error state.
Definition expected.h:30
Example — caller side:
auto result = cholesky(A);
if (!result) {
handle(result.error());
return;
}
auto& L = *result;

Member Typedef Documentation

◆ error_type

template<typename T , typename E >
using Expected< T, E >::error_type = E

◆ value_type

template<typename T , typename E >
using Expected< T, E >::value_type = T

Constructor & Destructor Documentation

◆ Expected() [1/4]

template<typename T , typename E >
Expected< T, E >::Expected ( T &&  v)
inlinenoexcept

Construct in the success state by moving a value in.

Parameters
vThe success value (moved from).

◆ Expected() [2/4]

template<typename T , typename E >
Expected< T, E >::Expected ( Unexpected< E >  u)
inlinenoexcept

Construct in the error state from an Unexpected tag.

Parameters
uAn Unexpected wrapper carrying the error code.

◆ Expected() [3/4]

template<typename T , typename E >
Expected< T, E >::Expected ( Expected< T, E > &&  other)
inlinenoexcept

Move constructor.

Placement-new constructs the active member from the source. The source is left in a valid but unspecified state.

Here is the call graph for this function:

◆ Expected() [4/4]

template<typename T , typename E >
Expected< T, E >::Expected ( const Expected< T, E > &  )
delete

Deleted — Expected is move-only to prevent accidental copies.

◆ ~Expected()

template<typename T , typename E >
Expected< T, E >::~Expected ( )
inlinenoexcept

Destructor. Destroys the active union member.

Member Function Documentation

◆ error()

template<typename T , typename E >
E Expected< T, E >::error ( ) const
inlinenoexcept

Access the error code.

Warning
Undefined behavior if in the success state.

◆ has_value()

template<typename T , typename E >
bool Expected< T, E >::has_value ( ) const
inlinenoexcept

Returns true if the Expected holds a success value.

◆ operator bool()

template<typename T , typename E >
Expected< T, E >::operator bool ( ) const
inlineexplicitnoexcept

Returns true if the Expected holds a success value.

◆ operator*() [1/2]

template<typename T , typename E >
const T & Expected< T, E >::operator* ( ) const
inlinenoexcept

Dereference operator (const) — access the success value.

Warning
Undefined behavior if in the error state.

◆ operator*() [2/2]

template<typename T , typename E >
T & Expected< T, E >::operator* ( )
inlinenoexcept

Dereference operator — access the success value.

Warning
Undefined behavior if in the error state.

◆ operator->() [1/2]

template<typename T , typename E >
const T * Expected< T, E >::operator-> ( ) const
inlinenoexcept

Arrow operator (const) — access members of the success value.

Warning
Undefined behavior if in the error state.

◆ operator->() [2/2]

template<typename T , typename E >
T * Expected< T, E >::operator-> ( )
inlinenoexcept

Arrow operator — access members of the success value.

Warning
Undefined behavior if in the error state.

◆ operator=() [1/2]

template<typename T , typename E >
Expected & Expected< T, E >::operator= ( const Expected< T, E > &  )
delete

Deleted — Expected is move-only to prevent accidental copies.

◆ operator=() [2/2]

template<typename T , typename E >
Expected & Expected< T, E >::operator= ( Expected< T, E > &&  other)
inlinenoexcept

Move assignment.

Destroys the current active member, then placement-new constructs from the source.

Here is the call graph for this function:

◆ value() [1/2]

template<typename T , typename E >
const T & Expected< T, E >::value ( ) const
inlinenoexcept

Access the success value by const reference.

Warning
Undefined behavior if in the error state.

◆ value() [2/2]

template<typename T , typename E >
T & Expected< T, E >::value ( )
inlinenoexcept

Access the success value by reference.

Warning
Undefined behavior if in the error state.

Member Data Documentation

◆ err_

template<typename T , typename E >
E Expected< T, E >::err_

◆ val_

template<typename T , typename E >
T Expected< T, E >::val_

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