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();
}

See also

Expected<void>

Template Parameters:

T – The success type. Must be Any-compatible and cannot be Error. The void type is supported as a success state without a value.

Public Functions

~Expected() = default#
inline Expected(T value)#

Implicit constructor from a success value.

Parameters:

value – The success value.

template<typename U, typename = std::enable_if_t<!details::is_expected_v<std::decay_t<U>> && !details::is_unexpected_v<std::decay_t<U>> && !std::is_base_of_v<Error, std::decay_t<U>> && std::is_convertible_v<U, T>>>
inline Expected(U &&value)#

Implicit constructor from a different success value type.

Template Parameters:

U – Source type implicitly convertible to T.

Parameters:

value – The success value to convert.

template<typename U, typename = std::enable_if_t<!std::is_void_v<U> && (type_subsumes_v<T, U> || std::is_convertible_v<U, T>)>>
inline Expected(Expected<U> other)#

Implicit converting constructor from another Expected success type.

Template Parameters:

U – Source success type whose storage is subsumed by or implicitly convertible to T.

Parameters:

other – The Expected value to convert.

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 int32_t type_index() const noexcept#

Return the raw stored type index.

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.

template<typename U, typename = std::enable_if_t<std::is_same_v<U, std::decay_t<U>> && !std::is_base_of_v<Error, U> && (TypeTraits<U>::storage_enabled || std::is_same_v<U, Any>)>>
inline Expected<U> as_or_error() const &#

Strictly reinterpret the success value as U, or return an error.

Note

This function does not perform value conversions. The source is preserved.

Template Parameters:

U – An unqualified owning storage type, excluding Error and its subclasses and void.

Returns:

A copy of the stored value or existing error, or TypeError on a type mismatch.

template<typename U, typename = std::enable_if_t<std::is_same_v<U, std::decay_t<U>> && !std::is_base_of_v<Error, U> && (TypeTraits<U>::storage_enabled || std::is_same_v<U, Any>)>>
inline Expected<U> as_or_error() &&#

Strictly reinterpret the success value as U, or return an error, moving the storage.

Note

This function does not perform value conversions. A type mismatch preserves the source.

Template Parameters:

U – An unqualified owning storage type, excluding Error and its subclasses and void.

Returns:

The moved stored value or existing error, or TypeError on a type mismatch.

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.