Skip to content

Struct nebula::util::expected

template <typename T, typename E>

ClassList > nebula > util > expected

A poor man's backport of C++23's std::expected. More...

  • #include <expected.hpp>

Public Functions

Type Name
E error ()
Retrieve the error, or throw bad_expected_access if a value is contained.
E error_or (const E & default_)
Return the contained error, or, if a value is contained, return the given default_ instead.
expected (const T & value)
expected (const E & error)
bool has_value ()
Whether the expected instance holds a value (as opposed to an error). Call this before trying to access values via value() .
T value ()
Retrieve the value, or throw bad_expected_access if an error is contained.
T value_or (const T & default_)
Return the contained value, or, if an error is contained, return the given default_ instead.
T value_or_throw (const std::string & error_msg)
If the instance has a value, return the value, else throw std::runtime_error(error_msg)

Detailed Description

At any given time, holds exactly one of: expected value, or error value. Provides functions for easy exception handling.

More info here: https://en.cppreference.com/w/cpp/utility/expected

Template parameters:

  • T Type of the expected value
  • E Error type

Public Functions Documentation

function error

Retrieve the error, or throw bad_expected_access if a value is contained.

inline E nebula::util::expected::error () 

Returns:

The error of type E


function error_or

Return the contained error, or, if a value is contained, return the given default_ instead.

inline E nebula::util::expected::error_or (
    const E & default_
) 

Returns:

The contained error, if any, else default_


function expected [1/2]

inline nebula::util::expected::expected (
    const T & value
) 

function expected [2/2]

inline nebula::util::expected::expected (
    const E & error
) 

function has_value

Whether the expected instance holds a value (as opposed to an error). Call this before trying to access values via value() .

inline bool nebula::util::expected::has_value () 

Returns:

True if a value is contained, false if an error is contained


function value

Retrieve the value, or throw bad_expected_access if an error is contained.

inline T nebula::util::expected::value () 

Returns:

The value of type T


function value_or

Return the contained value, or, if an error is contained, return the given default_ instead.

inline T nebula::util::expected::value_or (
    const T & default_
) 

Returns:

The contained value, if any, else default_


function value_or_throw

If the instance has a value, return the value, else throw std::runtime_error(error_msg)

inline T nebula::util::expected::value_or_throw (
    const std::string & error_msg
) 

Parameters:

  • error_msg The message to be included in the thrown exception

Returns:

The value



The documentation for this class was generated from the following file nebula_common/include/nebula_common/util/expected.hpp