On this page
std::expected<T,E>::expected
|
(1) | (since C++23) |
|
(2) | (since C++23) |
|
(3) | (since C++23) |
|
(4) | (since C++23) |
|
(5) | (since C++23) |
|
(6) | (since C++23) (T is not cv void) |
|
(7) | (since C++23) |
|
(8) | (since C++23) |
|
(9) | (since C++23) (T is not cv void) |
|
(10) | (since C++23) (T is not cv void) |
|
(11) | (since C++23) (T is cv void) |
|
(12) | (since C++23) |
|
(13) | (since C++23) |
Constructs a new expected object.
T is not (possibly cv-qualified) void, constructs an object that contains an expected value, which is value-initialized.
After construction, has_value() returns true.
- This overload participates in overload resolution only if
Tis (possibly cv-qualified) void orstd::is_default_constructible_v<T>istrue.
other.has_value() is false, the new object contains an unexpected value, which is direct-initialized from other.error(). Otherwise, if T is not (possibly cv-qualified) void, the new object contains an expected value, which is direct-initialized from *other.
After construction, has_value() is equal to other.has_value().
- This constructor is defined as deleted unless
- either
Tis (possibly cv-qualified) void, orstd::is_copy_constructible_v<T>istrue, and std::is_copy_constructible_v<E>istrue.
- either
- This constructor is trivial if
- either
Tis (possibly cv-qualified) void, orstd::is_trivially_copy_constructible_v<T>istrue, and std::is_trivially_copy_constructible_v<E>istrue.
- either
other.has_value() is false, the new object contains an unexpected value, which is direct-initialized from std::move(other.error()). Otherwise, if T is not (possibly cv-qualified) void, the new object contains an expected value, which is direct-initialized from std::move(*other).
After construction, has_value() is equal to other.has_value().
- This constructor participates in overload resolution only if
- either
Tis (possibly cv-qualified) void, orstd::is_move_constructible_v<T>istrue, and std::is_move_constructible_v<E>istrue.
- either
- This constructor is trivial if
std::is_trivially_move_constructible_v<T>istrue, andstd::is_trivially_move_constructible_v<E>istrue.
UFbe std::add_lvalue_reference_t<const U> for (4) andUfor (5), andGFbe const G& for (4) andGfor (5).
If other.has_value() is false, the new object contains an unexpected value, which is direct-initialized from std::forward<GF>(other.error()). Otherwise, if T is not (possibly cv-qualified) void, the new object contains an expected value, which is direct-initialized from std::forward<UF>(*other). After construction, has_value() is equal to other.has_value().
- Each of these constructors does not participate in overload resolution unless the following conditions are met respectively:
- Either
Tis (possibly cv-qualified) void, andstd::is_void_v<U>istrue, orstd::is_constructible_v<T, UF>istrue.
std::is_constructible_v<E, GF>istrue.- If
Tis not (possibly cv-qualified bool),Tis not constructible or convertible from any expression of type (possibly const) std::expected<U, G>, i.e., the following 8 values are allfalse:std::is_constructible_v<T, std::expected<U, G>&std::is_constructible_v<T, std::expected<U, G>std::is_constructible_v<T, const std::expected<U, G>&std::is_constructible_v<T, const std::expected<U, G>std::is_convertible_v<std::expected<U, G>&, T>std::is_convertible_v<std::expected<U, G>, T>std::is_convertible_v<const std::expected<U, G>&, T>std::is_convertible_v<const std::expected<U, G>, T>
- std::unexpected<E> is not constructible from any expression of type (possibly const) std::expected<U, G>, i.e., the following 4 values are all
false:std::is_constructible_v<std::unexpected<E>, std::expected<U, G>&std::is_constructible_v<std::unexpected<E>, std::expected<U, G>std::is_constructible_v<std::unexpected<E>, const std::expected<U, G>&std::is_constructible_v<std::unexpected<E>, const std::expected<U, G>
- Either
- These constructors are explicit if
std::is_convertible_v<UF, T>orstd::is_convertible_v<GF, E>isfalse.
T with the expression std::forward<U>(v).
After construction, has_value() returns true.
- This constructor does not participate in overload resolution unless the following conditions are met:
Tis not (possibly cv-qualified) void.std::is_same_v<std::remove_cvref_t<U>, std::in_place_t>isfalse.std::is_same_v<expected, std::remove_cvref_t<U>>isfalse.std::is_constructible_v<T, U>istrue.- std::remove_cvref_t<U> is not a specialization of
std::unexpected. - If
Tis (possibly cv-qualified) bool, std::remove_cvref_t<U> is not a specialization ofstd::expected.
GF be const G& for (7) and G for (8).
Constructs an object that contains an unexpected value, which is direct-initialized from std::forward<GF>(e.error()). After construction, has_value() returns false.
- These overloads participate in overload resolution only if
std::is_constructible_v<E, GF>istrue.
std::forward<Args>(args)....
After construction, has_value() returns true.
- This overload participates in overload resolution only if
std::is_constructible_v<T, Args...>istrue.
il, std::forward<Args>(args)....
After construction, has_value() returns true.
- This overload participates in overload resolution only if
std::is_constructible_v<T, std::initializer_list<U>&, Args...>istrue.
std::forward<Args>(args)....
After construction, has_value() returns false.
- This overload participates in overload resolution only if
std::is_constructible_v<E, Args...>istrue.
il, std::forward<Args>(args)....
After construction, has_value() returns false.
- This overload participates in overload resolution only if
std::is_constructible_v<E, std::initializer_list<U>&, Args...>istrue.
Parameters
| other | - | another expected object whose contained value is copied |
| e | - | std::unexpected object whose contained value is copied |
| v | - | value with which to initialize the contained value |
| args... | - | arguments with which to initialize the contained value |
| il | - | initializer list with which to initialize the contained value |
Exceptions
T. If T is (possibly cv-qualified) void,
noexcept specification:
noexcept
T or E.
T is (possibly cv-qualified) void,
noexcept specification:
noexcept(std::is_nothrow_move_constructible_v<E>)
noexcept specification:
noexcept(std::is_nothrow_move_constructible_v<T>
&& std::is_nothrow_move_constructible_v<E>)
T or E.
T.
E.
T.
E.
Example
See also
|
(C++23)
|
represented as an unexpected value (class template) |
|
(C++17)
|
in-place construction tag (tag) |
|
(C++23)
|
in-place construction tag for unexpected value in expected(tag) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/expected/expected