Template Class Expected#
Defined in File expected.h
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
- Template Parameters:
T – The success type. Must be Any-compatible and cannot be Error. The
voidtype 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.
-
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() &&#
Returns the contained error (moved out), or throws RuntimeError if is_ok().