Skip to content
class

Result

Result

Value-or-error container that holds either a T (success) or an ErrorPayload (failure) in a single std::variant.

Use Result<T> instead of an exception when a failure must be propagated across a boundary that cannot safely unwind C++ exceptions: the pybind11 FFI surface (when noexcept is required), async callbacks dispatched onto a worker queue, or any C ABI shim. Inside the engine itself, ops typically throw a LucidError directly because the stack-unwinding cost is acceptable and the typed exception carries richer state.

Invariants

  • is_ok() ⟺ data_ holds a T (variant index 0).
  • is_err() ⟺ data_ holds an ErrorPayload (variant index 1).

Thread safety

Result<T> instances are not internally synchronised. Sharing a single instance across threads requires external coordination.

See Also

Ok : Factory that wraps a value as a successful Result. Err : Factory that builds an ErrorPayload, implicitly convertible to Result<T> for any T.

Parameters

Ttypename
Type carried by the success arm. Must be movable; otherwise no constraints.

Constructors

1
ctor

Result<T>

void Result<T>(T value)

Constructs a successful result wrapping the given value.

Parameters

valueT
Payload moved into the success arm.

Methods

5
fnconst

error

→const ErrorPayload&
const ErrorPayload & error()

Accesses the stored error payload (const lvalue overload).

Returns

const ErrorPayload&

Const reference to the error descriptor. Lifetime is tied to *this.

Raises

:bad_variant_access
If is_ok is true.
fnconst

is_err

→bool
bool is_err()

Returns whether this result holds an error.

Returns

bool

true if the error arm is active, false otherwise.

fnconst

is_ok

→bool
bool is_ok()

Returns whether this result holds a value.

Returns

bool

true if the success arm is active, false otherwise.

fn

value

→T&
T & value()

Accesses the stored value (lvalue overload).

Returns

T&

Reference to the success payload. Lifetime is tied to *this.

Raises

:bad_variant_access
If is_err is true. In release builds this is equivalent to undefined behaviour — call is_ok first.
fn

value_or_throw

→T
T value_or_throw()

Returns the stored value or throws the error as a LucidError.

The rvalue-ref qualifier means this method consumes the Result — callers should write std::move(r).value_or_throw() (or call it on a prvalue) to avoid an unnecessary copy of T.

Returns

T

The success payload, moved out of the variant.

Raises

LucidError
If the error arm is active. The payload's msg is forwarded to ErrorBuilder::fail, which prepends "Result: " and the current ErrorContext trace.

Examples

```
T x = std::move(compute()).value_or_throw();
```