Skip to content

iox::cxx::expected< ValueType, ErrorType >🔗

specialization of the expected class which can contain an error as well as a success value More...

#include <expected.hpp>

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< ValueType > & successValue)
constructs an expected which is signaling success and uses the value provided by successValue to copy construct its success value
expected(success< ValueType > && successValue)
constructs an expected which is signaling success and uses the value provided by successValue to move construct its success value
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 errorValue
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
ValueType & value()
returns a reference to the contained success value, if the expected does not contain a success value this is undefined behavior
const ValueType & value() const
returns a const reference to the contained success value, if the expected does not contain a success value this is undefined behavior
ValueType && value()
returns a reference to the contained success value, if the expected does not contain a success value this is undefined behavior
ValueType value_or(const ValueType & value) const
returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value
ValueType value_or(const ValueType & value)
returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value
ValueType & operator*()
dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.
const ValueType & operator*() const
dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.
ValueType * operator->()
arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.
const ValueType * operator->() const
arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.
template \<typename T >
operator expected< T >()
conversion operator to an error only expected which can be useful if you would like to return only the success of a function
template \<typename T >
operator expected< T >() const
conversion operator to an error only expected which can be useful if you would like to return only the success of a function
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(ValueType &)> & callable) const
if the expected does contain a success value the given callable is called and a reference to the result is given as an argument to the callable
expected & and_then(const cxx::function_ref< void(ValueType &)> & callable)
if the expected does contain a success value the given callable is called and a reference to the result is given as an argument to the callable
template \<typename Optional =ValueType,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type =0>
const expected &
and_then(const cxx::function_ref< void(typename Optional::type &)> & callable) const
if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable
template \<typename Optional =ValueType,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type =0>
expected &
and_then(const cxx::function_ref< void(typename Optional::type &)> & callable)
if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable
template \<typename Optional =ValueType,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type =0>
const expected &
if_empty(const cxx::function_ref< void()> & callable) const
if the expected contains a success value and its type is an empty optional, calls the provided callable
template \<typename Optional =ValueType,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type =0>
expected &
if_empty(const cxx::function_ref< void()> & callable)
if the expected contains a success value and its type is an empty optional, calls the provided callable
optional< ValueType > to_optional() const
template \<typename... Targs>
expected< ValueType, ErrorType >
create_value(Targs &&... args)
template \<typename... Targs>
expected< ValueType, ErrorType >
create_error(Targs &&... args)
template \<typename Optional ,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type >
const expected< ValueType, ErrorType > &
and_then(const cxx::function_ref< void(typename Optional::type &)> & callable) const
template \<typename Optional ,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type >
expected< ValueType, ErrorType > &
and_then(const cxx::function_ref< void(typename Optional::type &)> & callable)
template \<typename Optional ,typename std::enable_if< internal::IsOptional< Optional >::value, int >::type >
expected< ValueType, ErrorType > &
if_empty(const cxx::function_ref< void(void)> & callable)
template \<typename... Targs>
expected
create_value(Targs &&... args)
creates an expected which is signaling success and perfectly forwards the args to the constructor of ValueType
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 ErrorType

Detailed Description🔗

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

specialization of the expected class which can contain an error as well as a success value

Parameters:

  • ValueType type of the value which can be stored in the expected
  • ErrorType type of the error which can be stored in the expected

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🔗

inline expected(
    const success< ValueType > & successValue
)

constructs an expected which is signaling success and uses the value provided by successValue to copy construct its success value

Parameters:

  • successValue value which will be stored in the expected

function expected🔗

inline expected(
    success< ValueType > && successValue
)

constructs an expected which is signaling success and uses the value provided by successValue to move construct its success value

Parameters:

  • successValue value which will be moved into the expected

function expected🔗

inline 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🔗

inline expected(
    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 moved into the expected

function operator bool🔗

inline 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🔗

inline 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🔗

inline 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 value🔗

inline ValueType & value()

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

Return: reference to the internally contained value

function value🔗

const ValueType & value() const

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

Return: const reference to the internally contained value

function value🔗

ValueType && value()

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

Return: rvalue reference to the internally contained value

function value_or🔗

inline ValueType value_or(
    const ValueType & value
) const

returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value

Return: copy of the internally contained value or copy of value

function value_or🔗

inline ValueType value_or(
    const ValueType & value
)

returns a copy of the contained success value if the expected does contain a success value, otherwise it returns a copy of value

Return: copy of the internally contained value or copy of value

function operator*🔗

inline ValueType & operator*()

dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.

Return: reference to the contained value

cxx::expected<int, float> frodo(success<int>(45));
*frodo += 12;
std::cout << *frodo << std::endl; // prints 57

function operator*🔗

const ValueType & operator*() const

dereferencing operator which returns a reference to the contained success value. if the expected contains an error the behavior is undefined.

Return: const reference to the contained value

cxx::expected<int, float> frodo(success<int>(45));
*frodo += 12;
std::cout << *frodo << std::endl; // prints 57

function operator->🔗

inline ValueType * operator->()

arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.

Return: pointer of type ValueType to the contained value

cxx::expected<std::vector<int>, int> holyPiotr(success<std::vector<int>>({1,2,3}));
holyPiotr->push_back(4);

function operator->🔗

const ValueType * operator->() const

arrow operator which returns the pointer to the contained success value. if the expected contains an error the behavior is undefined.

Return: pointer of type const ValueType to the contained value

cxx::expected<std::vector<int>, int> holyPiotr(success<std::vector<int>>({1,2,3}));
holyPiotr->push_back(4);

function operator expected< T >🔗

template <typename T >
inline operator expected< T >()

conversion operator to an error only expected which can be useful if you would like to return only the success of a function

Return: converts an expected which can contain a value and an error to an expected which contains only an error

cxx::expected<int, int> someErrorProneFunction(){}

cxx::expected<int> isItSuccessful() {
    return someErrorProneFunction();
}

function operator expected< T >🔗

template <typename T >
operator expected< T >() const

conversion operator to an error only expected which can be useful if you would like to return only the success of a function

Return: converts an expected which can contain a value and an error to an expected which contains only an error

cxx::expected<int, int> someErrorProneFunction(){}

cxx::expected<int> isItSuccessful() {
    return someErrorProneFunction();
}

function or_else🔗

inline 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& result){
    std::cout << "error occured : " << error << std::endl;
})

function or_else🔗

inline 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: reference to the expected itself

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

function and_then🔗

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

if the expected does contain a success value the given callable is called and a reference to the result 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

someExpected.and_then([](int& result){
    std::cout << "we have a result : " << result << std::endl;
})

function and_then🔗

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

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

Parameters:

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

Return: reference to the expected

someExpected.and_then([](int& result){
    std::cout << "we have a result : " << result << std::endl;
})

function and_then🔗

template <typename Optional  =ValueType,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type  =0>
const expected & and_then(
    const cxx::function_ref< void(typename Optional::type &)> & callable
) const

if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable

Parameters:

  • callable the callable to be called with the contents of the optional

Return: reference to the expected

anExpectedOptional.and_then([](int& value){
    std::cout << "the optional contains the value: " << result << std::endl;
})

function and_then🔗

template <typename Optional  =ValueType,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type  =0>
expected & and_then(
    const cxx::function_ref< void(typename Optional::type &)> & callable
)

if the expected contains a success value and its type is a non-empty optional, retrieve the value from the optional and provide it as the argument to the provided callable

Parameters:

  • callable the callable to be called with the contents of the optional

Return: reference to the expected

anExpectedOptional.and_then([](int& value){
    std::cout << "the optional contains the value: " << result << std::endl;
})

function if_empty🔗

template <typename Optional  =ValueType,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type  =0>
const expected & if_empty(
    const cxx::function_ref< void()> & callable
) const

if the expected contains a success value and its type is an empty optional, calls the provided callable

Parameters:

  • callable the callable to be called if the contained optional is empty

Return: reference to the expected

anExpectedOptional.and_then([](SomeType& value){
        std::cout << "we got something in the optional: " << value << std::endl;
    })
    .if_empty([](){
        std::cout << "the optional was empty, but do something anyway!" << result << std::endl;
    })

function if_empty🔗

template <typename Optional  =ValueType,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type  =0>
expected & if_empty(
    const cxx::function_ref< void()> & callable
)

if the expected contains a success value and its type is an empty optional, calls the provided callable

Parameters:

  • callable the callable to be called if the contained optional is empty

Return: reference to the expected

anExpectedOptional.and_then([](SomeType& value){
        std::cout << "we got something in the optional: " << value << std::endl;
    })
    .if_empty([](){
        std::cout << "the optional was empty, but do something anyway!" << result << std::endl;
    })

function to_optional🔗

inline optional< ValueType > to_optional() const

function create_value🔗

template <typename... Targs>
inline expected< ValueType, ErrorType > create_value(
    Targs &&... args
)

function create_error🔗

template <typename... Targs>
inline expected< ValueType, ErrorType > create_error(
    Targs &&... args
)

function and_then🔗

template <typename Optional ,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type >
inline const expected< ValueType, ErrorType > & and_then(
    const cxx::function_ref< void(typename Optional::type &)> & callable
) const

function and_then🔗

template <typename Optional ,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type >
inline expected< ValueType, ErrorType > & and_then(
    const cxx::function_ref< void(typename Optional::type &)> & callable
)

function if_empty🔗

template <typename Optional ,
typename std::enable_if< internal::IsOptional< Optional >::value, int >::type >
inline expected< ValueType, ErrorType > & if_empty(
    const cxx::function_ref< void(void)> & callable
)

function create_value🔗

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

creates an expected which is signaling success and perfectly forwards the args to the constructor of ValueType

Parameters:

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

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 ErrorType

Parameters:

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

Return: expected signalling error


Updated on 17 June 2021 at 11:15:26 CEST