Template Class Expected#

Class Documentation#

template<typename T>
class Expected#

Expected<T> provides exception-free error handling for FFI functions.

Expected<T> is similar to Rust’s Result<T, Error> or C++23’s std::expected. It can hold either a success value of type T or an error of type Error.

Usage:

Expected<int> divide(int a, int b) {
  if (b == 0) {
    return Error("ValueError", "Division by zero");
  }
  return a / b;
}

Expected<int> result = divide(10, 2);
if (result.is_ok()) {
  int value = result.value();
} else {
  Error err = result.error();
}

Template Parameters:

T – The success type. Must be Any-compatible and cannot be Error.

Public Functions

inline Expected(T value)#

Implicit constructor from a success value.

Parameters:

value – The success value.

inline Expected(Error error)#

Implicit constructor from an error.

Parameters:

error – The error value.

template<typename E, typename = std::enable_if_t<std::is_base_of_v<Error, std::remove_cv_t<E>>>>
inline Expected(Unexpected<E> unexpected)#

Implicit constructor from an Unexpected wrapper.

inline bool is_ok() const noexcept#

Returns true if the Expected contains a success value.

inline bool is_err() const noexcept#

Returns true if the Expected contains an error.

inline bool has_value() const noexcept#

Alias for is_ok().

inline T value() const &#

Returns the success value, or throws the contained error.

inline T value() &&#

Returns the success value (moved out), or throws the contained error.

inline Error error() const &#

Returns the contained error, or throws RuntimeError if is_ok().

inline Error error() &&#

Returns the contained error (moved out), or throws RuntimeError if is_ok().

template<typename U = std::remove_cv_t<T>>
inline T value_or(U &&default_value) const &#

Returns the success value, or default_value if the Expected holds an error.

template<typename U = std::remove_cv_t<T>>
inline T value_or(U &&default_value) &&#

Returns the success value (moved out), or default_value if the Expected holds an error.