template<typename T>
class etcpal::Expected< T >
A type representing either a value or an etcpal_error_t code.
This class is modeled after the std::expected proposal. It is a useful companion to C++17's std::optional, when one desires a concise representation of either a value or an error code representing why that value is not present.
The proposed std::expected is generic with respect to both the value and the error type, but EtcPal's version always uses etcpal_error_t as the error. This makes it useful in the context of EtcPal itself and libraries that depend on EtcPal; if you are looking for a generically-useful expected
implementation, martinmoene's version is recommended. This implementation is based heavily on that one.
Usage
Expected is almost always used as the return type of an API function. Consider a function that either creates a network socket or returns an error.
A type representing either a value or an etcpal_error_t code.
Definition: error.h:477
When using this function, check whether the result contains an error:
auto socket_res = CreateSocket(AF_INET, SOCK_STREAM);
if (socket_res)
{
}
else
{
printf("CreateSocket failed with result code %d, description '%s'\n", socket_res.error(),
socket_res.result().ToCString());
}
PLATFORM_DEFINED etcpal_socket_t
A socket handle.
Definition: socket.dox:11
value() throws BadExpectedAccess if the instance does not have a valid value. So you can also use an exception-handling approach:
try
{
}
{
printf("CreateSocket failed with result code %d, description '%s'\n", e.result().code(),
e.result().ToCString());
}
Exception representing bad access to an Expected instance.
Definition: error.h:218
Note that this only works for the value() function, not any of the other accessors (they assert on the correct state instead).
Inside functions that return Expected, you can use implicit conversions from the value type or etcpal_error_t to return success or failure:
{
return socket;
else
return res;
}
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
@ kEtcPalErrOk
The call was successful, no error occurred.
Definition: error.h:51
etcpal_error_t etcpal_socket(unsigned int family, unsigned int type, etcpal_socket_t *id)
Create a socket.
The value_or() function can be useful when you want to use a default value in case of an error condition:
std::string conversion = ConvertIntToString(arg).value_or("0");
|
template<bool B = std::is_default_constructible<T>::value, typename std::enable_if< B, int >::type = 0> |
ETCPAL_CONSTEXPR_14 | Expected () |
| Default constructor - enabled if T is default-constructible. More...
|
|
ETCPAL_CONSTEXPR_14 | Expected (const Expected &other)=default |
| Copy constructor - enabled if T is copy-constructible. More...
|
|
ETCPAL_CONSTEXPR_14 | Expected (Expected &&other)=default |
| Move constructor - enabled if T is move-constructible. More...
|
|
template<typename U > |
ETCPAL_CONSTEXPR_14 | Expected (const Expected< U > &other, ETCPAL_ENABLE_IF_ARG(std::is_constructible< T, const U & >::value &&!std::is_constructible< T, Expected< U > & >::value &&!std::is_constructible< T, Expected< U > && >::value &&!std::is_constructible< T, Expected< U > const & >::value &&!std::is_constructible< T, Expected< U > const && >::value &&!std::is_convertible< Expected< U > &, T >::value &&!std::is_convertible< Expected< U > &&, T >::value &&!std::is_convertible< Expected< U > const &, T >::value &&!std::is_convertible< Expected< U > const &&, T >::value &&!std::is_convertible< const U &, T >::value)) |
| Explicit conversion copy constructor - enabled for U that can be explicitly converted to T. More...
|
|
template<typename U > |
ETCPAL_CONSTEXPR_14 | Expected (const Expected< U > &other, ETCPAL_ENABLE_IF_ARG(std::is_constructible< T, const U & >::value &&!std::is_constructible< T, Expected< U > & >::value &&!std::is_constructible< T, Expected< U > && >::value &&!std::is_constructible< T, Expected< U > const & >::value &&!std::is_constructible< T, Expected< U > const && >::value &&!std::is_convertible< Expected< U > &, T >::value &&!std::is_convertible< Expected< U > &&, T >::value &&!std::is_convertible< Expected< U > const &, T >::value &&!std::is_convertible< Expected< U > const &&, T >::value &&std::is_convertible< const U &, T >::value)) |
| Implicit conversion copy constructor - enabled for U that can be implicitly converted to T. More...
|
|
template<typename U > |
ETCPAL_CONSTEXPR_14 | Expected (Expected< U > &&other, ETCPAL_ENABLE_IF_ARG(std::is_constructible< T, U >::value &&!std::is_constructible< T, Expected< U > & >::value &&!std::is_constructible< T, Expected< U > && >::value &&!std::is_constructible< T, Expected< U > const & >::value &&!std::is_constructible< T, Expected< U > const && >::value &&!std::is_convertible< Expected< U > &, T >::value &&!std::is_convertible< Expected< U > &&, T >::value &&!std::is_convertible< Expected< U > const &, T >::value &&!std::is_convertible< Expected< U > const &&, T >::value &&!std::is_convertible< U, T >::value)) |
| Explicit conversion move constructor - enabled for U that can be explicitly converted to T. More...
|
|
template<typename U > |
ETCPAL_CONSTEXPR_14 | Expected (Expected< U > &&other, ETCPAL_ENABLE_IF_ARG(std::is_constructible< T, U >::value &&!std::is_constructible< T, Expected< U > & >::value &&!std::is_constructible< T, Expected< U > && >::value &&!std::is_constructible< T, Expected< U > const & >::value &&!std::is_constructible< T, Expected< U > const && >::value &&!std::is_convertible< Expected< U > &, T >::value &&!std::is_convertible< Expected< U > &&, T >::value &&!std::is_convertible< Expected< U > const &, T >::value &&!std::is_convertible< Expected< U > const &&, T >::value &&std::is_convertible< U, T >::value)) |
| Implicit conversion move constructor - enabled for U that can be implicitly converted to T. More...
|
|
template<typename U = T> |
ETCPAL_CONSTEXPR_14 | Expected (U &&value, ETCPAL_ENABLE_IF_ARG(std::is_constructible< T, U && >::value &&!std::is_convertible< U &&, T >::value)) noexcept(std::is_nothrow_move_constructible< U >::value) |
| Construct from value, explicit. More...
|
|
template<typename U = T> |
ETCPAL_CONSTEXPR_14 | Expected (U &&value, ETCPAL_ENABLE_IF_ARG(std::is_constructible< T, U && >::value &&std::is_convertible< U &&, T >::value)) noexcept(std::is_nothrow_move_constructible< U >::value) |
| Construct from value, implicit. More...
|
|
ETCPAL_CONSTEXPR_14 | Expected (etcpal_error_t error) |
| Construct an Expected instance containing an error code.
|
|
| ~Expected () |
| Calls the contained value's destructor if and only if has_value() is true.
|
|
constexpr const T * | operator-> () const |
| Access members of a composite contained value. More...
|
|
T * | operator-> () |
| Access members of a composite contained value. More...
|
|
constexpr const T & | operator* () const & |
| Get the underlying value. More...
|
|
T & | operator* () & |
| Get the underlying value. More...
|
|
constexpr const T && | operator* () const && |
| Get the underlying value. More...
|
|
ETCPAL_CONSTEXPR_14 T && | operator* () && |
| Get the underlying value. More...
|
|
constexpr | operator bool () const noexcept |
| Evaluate the Expected instance inline - evaluates to has_value().
|
|
constexpr bool | has_value () const noexcept |
| Whether this Expected instance contains a valid value. If not, contains an error code.
|
|
ETCPAL_CONSTEXPR_14 const T & | value () const & |
| Get the underlying value. More...
|
|
T & | value () & |
| Get the underlying value. More...
|
|
ETCPAL_CONSTEXPR_14 const T && | value () const && |
| Get the underlying value. More...
|
|
ETCPAL_CONSTEXPR_14 T && | value () && |
| Get the underlying value. More...
|
|
constexpr etcpal_error_t | error_code () const noexcept |
| Get the error code. More...
|
|
constexpr Error | error () const noexcept |
| Get the error code as a Error object. More...
|
|
template<typename U , ETCPAL_ENABLE_IF_TEMPLATE(std::is_copy_constructible< T >::value &&std::is_convertible< U &&, T >::value) > |
constexpr T | value_or (U &&def_val) const & |
| Get the value, or a default value if this Expected contains an error. More...
|
|
template<typename U , ETCPAL_ENABLE_IF_TEMPLATE(std::is_move_constructible< T >::value &&std::is_convertible< U &&, T >::value) > |
T | value_or (U &&def_val) && |
| Get the value, or a default value if this Expected contains an error. More...
|
|