Template Class Result

Class Documentation

template<typename T, typename E = std::error_code>
class Acts::Result

Class which encapsulates either a valid result, or an error.

Template Parameters
  • T: The valid result value

  • E: The error, defaults to std::error_code

Public Functions

Result() = delete

Default construction is disallowed.

Result(const Result<T, E> &other) = delete

Copy construction is disallowed.

Result(Result<T, E> &&other)

Move construction is allowed.

template<typename T2, typename _E = E, typename _T = T, typename = std::enable_if_t<(!std::is_same_v<_T, _E> && !std::is_constructible_v<_T, _E> && !std::is_convertible_v<_T, _E> && !std::is_constructible_v<_E, _T> && !std::is_convertible_v<_E, _T> && !(std::is_convertible_v<T2, _T> && std::is_convertible_v<T2, _E>))>>
Result(T2 value) noexcept

Constructor from arbitrary value This constructor allows construction from any value.

This constructor is only enabled if T and E are unambiguous, meaning the cannot be implicitly converted and there is T cannot be constructed from E and vice-versa. This means that when this is invoked, the value can be propagated to the underlying variant, and the assignment will be correct, and error will be an error, and a value will be a value.

Note

If T and E are ambigious, use the success and failure static factory methods.

Template Parameters
  • T2: Type of the potential assignment

Parameters
  • value: The potential value, could be an actual valid value or an error.

E &error() & noexcept

Returns a reference to the error stored in the result.

Note

If res.ok() this method will abort (noexcept)

Return

Reference to the error

E error() && noexcept

Returns the error by-value.

Note

If res.ok() this method will abort (noexcept)

Return

The error

bool ok() const noexcept

Checks whether this result contains a valid value, and no error.

Return

bool Whether result contains an error or not.

T &operator*() noexcept

Returns a reference into the variant to the valid value.

Note

If !res.ok(), this method will abort (noexcept)

Return

Reference to value stored in the variant.

Result<T, E> &operator=(const Result<T, E> &other) = delete

Assignment is disallowed.

Result<T, E> &operator=(Result<T, E> &&other)

Move assignment is allowed.

Return

The assigned instance

Parameters
  • other: The other result instance, rvalue reference

template<typename T2, typename _E = E, typename _T = T, typename = std::enable_if_t<(!std::is_same_v<_T, _E> && !std::is_constructible_v<_T, _E> && !std::is_convertible_v<_T, _E> && !std::is_constructible_v<_E, _T> && !std::is_convertible_v<_E, _T> && !(std::is_convertible_v<T2, _T> && std::is_convertible_v<T2, _E>))>>
Result<T, E> &operator=(T2 value) noexcept

Assignment operator from arbitrary value This operator allows construction from any value.

The same rules as for the Result(T2 value) constructor apply.

  • Return

    The assigned instance

    Template Parameters
    • T2: Type of the potential assignment

    Parameters
    • value: The potential value, could be an actual valid value or an error.

T &value() &

Retrieves the valid value from the result object.

Note

This is the lvalue version, returns a reference to the value

Return

The valid value as a reference

T value() &&

Retrieves the valid value from the result object.

Note

This is the rvalue version, returns the value by-value and moves out of the variant.

Return

The valid value by value, moved out of the variant.

Public Static Functions

Result<T, E> failure(E error)

Static helper factory which forces assignment as an error.

Return

Initialized result object

Parameters
  • value: The error to assign. Will not be converted to T.

Result<T, E> success(T value)

Static helper factory which forces assignment as valid value.

Return

Initialized result object

Parameters
  • value: The valid value to assign. Will not be converted to E.