Result<T, E> is a template type that can be used to return and propagate errors. The intent is to replace exceptions in this context. Result<T, E> is an algebraic data type of Ok(T) that represents success and Err(E) representing an error.
More...
|
| | Result (impl::Ok< T > &&ok) noexcept |
| | Construct a Result that indicates success and that carries a valid return value.
|
| | Result (impl::Err< E > &&err) noexcept |
| | Construct a Result that indicates failure and that carries an error value.
|
| | ~Result ()=default |
| template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, const U & >, std::is_constructible< E, const G & > >, bool > = true> |
| | Result (const Result< U, G > &other) |
| | Do a conversion construction from a compatible Result.
|
| template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, U >, std::is_constructible< E, G > >, bool > = true> |
| | Result (Result< U, G > &&other) |
| | Do a conversion construction from a compatible Result.
|
| bool | operator== (const Result &other) const |
| bool | operator!= (const Result &other) const |
| bool | isOk () const noexcept |
| | Used to determine if the result is not an error.
|
| bool | isError () const noexcept |
| | Used to determine if the result is an error.
|
| | operator bool () const noexcept |
| | Used to determine if the result is not an error.
|
| template<typename U = T> |
| const impl::NonVoidT< U > & | getValueOr (const U &defaultVal) const noexcept |
| | Used to determine the return value. If the result indicates an error this method will return defaultValue.
|
| template<typename U = T> |
| const impl::NonVoidT< U > & | getValue () const & |
| | Used to determine the success value, given that there is no error.
|
| template<typename U = T> |
| impl::NonVoidT< U > && | getValue () && |
| | Used to move out the success value, given that there is no error.
|
| const E & | getError () const |
| | Used to determine the error value, given that there is an error.
|
| template<typename U = T> |
| const impl::NonVoidT< U > & | expect (std::string_view errorMsg={}) const noexcept |
| | Extracts the value of the correct result, or terminates the program with a given error message.
|
template<typename T, typename E>
class sen::Result< T, E >
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to replace exceptions in this context. Result<T, E> is an algebraic data type of Ok(T) that represents success and Err(E) representing an error.
Design of this class has been mainly inspired by Rust's std::result. Example:
enum class ErrorCode { too_small, too_large };
{
if (a < 0)
{
return Err(ErrorCode::too_small);
}
}
{
if (a < 0)
{
return Err(ErrorCode::too_small);
}
}
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Result(impl::Ok< T > &&ok) noexcept
Construct a Result that indicates success and that carries a valid return value.
Definition result.h:145
impl::Err< void > Err() noexcept
If E is void, use the void specialization of Err.
Definition result.h:439
impl::Ok< void > Ok() noexcept
If T is void, use the void specialization of Ok.
Definition result.h:434
Use isOk(), isError() or the bool() operator to check if there was an error. Use getError() to get the error data (terminates the program if there was no error). Use getValue() to determine the return value if there is no error (terminates the program otherwise). Use getValueOr(X) as getValue() but returns X if there was an error.
Example:
if (auto result = doSomething())
{
const auto& val = result.getValue();
}
else
{
auto err = result.getError();
}
- Note
- This class is not thread-aware.
- Template Parameters
-
| T | the type of the result if there is no error (can be void). |
| E | the type of the error. |
template<typename T, typename E>
template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, const U & >, std::is_constructible< E, const G & > >, bool > = true>
| sen::Result< T, E >::Result |
( |
const Result< U, G > & | other | ) |
|
|
inline |
Do a conversion construction from a compatible Result.
template<typename T, typename E>
template<typename U, typename G, std::enable_if_t< std::conjunction_v< std::is_constructible< T, U >, std::is_constructible< E, G > >, bool > = true>
Do a conversion construction from a compatible Result.