std::expected
From cppreference.com
Defined in header <expected>
|
||
template< class T, class E > class expected; |
(1) | (since C++23) |
template< class T, class E > requires std::is_void_v<T> |
(2) | (since C++23) |
The class template std::expected
provides a way to represent either of two values: an expected value of type T
, or an unexpected value of type E
. expected
is never valueless.
1) The main template. Contains the expected or unexpected value within its own storage, which is nested within the
expected
object.2) The void partial specialization. Represents an expected void value or contains an unexpected value. If it contains an unexpected value, it is nested within the
expected
object.A program is ill-formed if it instantiates an expected
with a reference type, a function type, or a specialization of std::unexpected. In addition, T
must not be std::in_place_t or std::unexpect_t.
Contents |
[edit] Template parameters
T | - | the type of the expected value. The type must either be (possibly cv-qualified) void, or meet the Destructible requirements (in particular, array and reference types are not allowed). |
E | - | the type of the unexpected value. The type must meet the Destructible requirements, and must be a valid template argument for std::unexpected (in particular, arrays, non-object types, and cv-qualified types are not allowed). |
[edit] Nested types
Type | Definition |
value_type
|
T
|
error_type
|
E
|
unexpected_type
|
std::unexpected<E>
|
[edit] Member templates
Template | Definition |
rebind<U> | std::expected<U, error_type> |
[edit] Data members
Member | Description |
bool has_val
|
whether the expected object currently represents the expected value(exposition-only member object*) |
T val (main template only)
|
the expected value (exposition-only variant member object*) |
E unex
|
the unexpected value (exposition-only variant member object*) |
[edit] Member functions
constructs the expected object (public member function) | |
destroys the expected object, along with its contained value (public member function) | |
assigns contents (public member function) | |
Observers | |
accesses the expected value (public member function) | |
checks whether the object contains an expected value (public member function) | |
returns the expected value (public member function) | |
returns the unexpected value (public member function) | |
returns the expected value if present, another value otherwise (public member function) | |
returns the unexpected value if present, another value otherwise (public member function) | |
Monadic operations | |
returns the result of the given function on the expected value if it exists; otherwise, returns the expected itself (public member function) | |
returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself (public member function) | |
returns the expected itself if it contains an expected value; otherwise, returns the result of the given function on the unexpected value (public member function) |