Skip to content

iox::cxx::expected< ErrorType >🔗

expected implementation from the C++20 proposal with C++11. The interface is inspired by the proposal but it has changes since we are not allowed to throw an exception. More...

#include <iceoryx_hoofs/cxx/expected.hpp>

Inherited by iox::cxx::expected< void, ErrorType >

Public Functions🔗

Name
expected()
default ctor is deleted since you have to clearly state if the expected contains a success value or an error value
expected(const expected & ) =default
the copy constructor calls the copy constructor of the contained success value or the error value - depending on what is stored in the expected
expected(expected && rhs)
the move constructor calls the move constructor of the contained success value or the error value - depending on what is stored in the expected
~expected() =default
calls the destructor of the success value or error value - depending on what is stored in the expected
expected & operator=(const expected & )
calls the copy assignment operator of the contained success value or the error value - depending on what is stored in the expected
expected & operator=(expected && rhs)
calls the move assignment operator of the contained success value or the error value - depending on what is stored in the expected
expected(const success< void > & successValue)
constructs an expected which is signaling success
expected(const error< ErrorType > & errorValue)
constructs an expected which is signaling an error and stores the error value provided by errorValue
expected(error< ErrorType > && errorValue)
constructs an expected which is signaling an error and stores the error value provided by value
operator bool() const
returns true if the expected contains an error otherwise false
bool has_error() const
returns true if the expected contains an error otherwise false
ErrorType & get_error()
returns a reference to the contained error value, if the expected does not contain an error this is undefined behavior
const ErrorType & get_error() const
returns a const reference to the contained error value, if the expected does not contain an error this is undefined behavior
ErrorType && get_error()
returns a rvalue reference to the contained error value, if the expected does not contain an error this is undefined behavior
const expected & or_else(const cxx::function_ref< void(ErrorType &)> & callable) const
if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable
expected & or_else(const cxx::function_ref< void(ErrorType &)> & callable)
if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable
const expected & and_then(const cxx::function_ref< void()> & callable) const
if the expected does contain a success value the given callable is called and a reference to the expected is given as an argument to the callable
expected & and_then(const cxx::function_ref< void()> & callable)
if the expected does contain a success value the given callable is called and a reference to the expected is given as an argument to the callable
expected create_value()
creates an expected which is signaling success
template <typename... Targs>
expected
create_error(Targs &&... args)
creates an expected which is signaling an error and perfectly forwards the args to the constructor of lErrorType

Detailed Description🔗

template <typename ErrorType >
class iox::cxx::expected< ErrorType >;

expected implementation from the C++20 proposal with C++11. The interface is inspired by the proposal but it has changes since we are not allowed to throw an exception.

Parameters:

  • ErrorType type of the error which can be stored in the expected
cxx::expected<int, float> callMe() {
    bool l_errorOccured;
    // ... do stuff
    if ( l_errorOccured ) {
        return cxx::error<float>(55.1f);
    } else if ( !l_errorOccured ) {
        return cxx::success<int>(123);
    }
}

cxx::expected<float> errorOnlyMethod() {
    return callMe().or_else([]{
        std::cerr << "Error Occured\n";
        /// perform some action
    }).and_then([](cxx::expected<int, float> & result){
        std::cout << "Success, got " << result.value() << std::endl;
        /// perform some action
    });
}

cxx::expected<std::vector<int>, int> allHailHypnotoad(success<std::vector<int>>({6,6,6}));
allHailHypnotoad->push_back(7);

Public Functions Documentation🔗

function expected🔗

expected()

default ctor is deleted since you have to clearly state if the expected contains a success value or an error value

function expected🔗

expected(
    const expected & 
) =default

the copy constructor calls the copy constructor of the contained success value or the error value - depending on what is stored in the expected

function expected🔗

expected(
    expected && rhs
)

the move constructor calls the move constructor of the contained success value or the error value - depending on what is stored in the expected

function ~expected🔗

~expected() =default

calls the destructor of the success value or error value - depending on what is stored in the expected

function operator=🔗

expected & operator=(
    const expected & 
)

calls the copy assignment operator of the contained success value or the error value - depending on what is stored in the expected

function operator=🔗

expected & operator=(
    expected && rhs
)

calls the move assignment operator of the contained success value or the error value - depending on what is stored in the expected

function expected🔗

expected(
    const success< void > & successValue
)

constructs an expected which is signaling success

Parameters:

  • successValue value which will be stored in the expected

function expected🔗

expected(
    const error< ErrorType > & errorValue
)

constructs an expected which is signaling an error and stores the error value provided by errorValue

Parameters:

  • errorValue error value which will be stored in the expected

function expected🔗

expected(
    error< ErrorType > && errorValue
)

constructs an expected which is signaling an error and stores the error value provided by value

Parameters:

  • errorValue error value which will be moved into the expected

function operator bool🔗

explicit operator bool() const

returns true if the expected contains an error otherwise false

Return: bool which contains true if the expected contains an error

function has_error🔗

bool has_error() const

returns true if the expected contains an error otherwise false

Return: bool which contains true if the expected contains an error

function get_error🔗

ErrorType & get_error()

returns a reference to the contained error value, if the expected does not contain an error this is undefined behavior

Return: reference to the internally contained error

function get_error🔗

const ErrorType & get_error() const

returns a const reference to the contained error value, if the expected does not contain an error this is undefined behavior

Return: const reference to the internally contained error

function get_error🔗

ErrorType && get_error()

returns a rvalue reference to the contained error value, if the expected does not contain an error this is undefined behavior

Return: rvalue reference to the internally contained error

function or_else🔗

const expected & or_else(
    const cxx::function_ref< void(ErrorType &)> & callable
) const

if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable

Parameters:

  • callable callable which will be called if the expected contains an error

Return: const reference to the expected itself

someExpected.or_else([](float& error){
    std::cout << "error occured : " << error << std::endl;
})

function or_else🔗

expected & or_else(
    const cxx::function_ref< void(ErrorType &)> & callable
)

if the expected does contain an error the given callable is called and a reference to the ErrorType is given as an argument to the callable

Parameters:

  • callable callable which will be called if the expected contains an error

Return: const reference to the expected itself

someExpected.or_else([](float& error){
    std::cout << "error occured : " << error << std::endl;
})

function and_then🔗

const expected & and_then(
    const cxx::function_ref< void()> & callable
) const

if the expected does contain a success value the given callable is called and a reference to the expected is given as an argument to the callable

Parameters:

  • callable callable which will be called if the expected contains a success value

Return: const reference to the expected itself

someExpected.and_then([]{
    std::cout << "we are successful!" << std::endl;
})

function and_then🔗

expected & and_then(
    const cxx::function_ref< void()> & callable
)

if the expected does contain a success value the given callable is called and a reference to the expected is given as an argument to the callable

Parameters:

  • callable callable which will be called if the expected contains a success value

Return: const reference to the expected itself

someExpected.and_then([]{
    std::cout << "we are successful!" << std::endl;
})

function create_value🔗

static expected create_value()

creates an expected which is signaling success

Return: expected signalling success

function create_error🔗

template <typename... Targs>
static expected create_error(
    Targs &&... args
)

creates an expected which is signaling an error and perfectly forwards the args to the constructor of lErrorType

Parameters:

  • args... arguments which will be forwarded to the ErrorType constructor

Return: expected signalling error


Updated on 18 December 2023 at 13:11:42 CET