| Primary template |
||
template< class F > constexpr auto or_else( F&& f ) &; |
(1) | (since C++23) |
template< class F > constexpr auto or_else( F&& f ) const&; |
(2) | (since C++23) |
template< class F > constexpr auto or_else( F&& f ) &&; |
(3) | (since C++23) |
template< class F > constexpr auto or_else( F&& f ) const&&; |
(4) | (since C++23) |
| void partial specialization |
||
template< class F > constexpr auto or_else( F&& f ) &; |
(5) | (since C++23) |
template< class F > constexpr auto or_else( F&& f ) const&; |
(6) | (since C++23) |
template< class F > constexpr auto or_else( F&& f ) &&; |
(7) | (since C++23) |
template< class F > constexpr auto or_else( F&& f ) const&&; |
(8) | (since C++23) |
If *this contains an unexpected value, invokes f with the unexpected value of *this as the argument and returns its result. Otherwise, returns a std::expected object that represents an expected value.
Given type G as:
If G is not a specialization of std::expected, or std::is_same_v<G::value_type, T> is false, the program is ill-formed.
std::is_constructible_v<T, decltype((val))> is true.std::is_constructible_v<T, decltype(std::move(val))> is true.Parameters
| f | - | a suitable function or Callable object that returns a std::expected
|
Return value
| Overload | Value of has_value()
| |
|---|---|---|
true
|
false
| |
| (1,2) | G(std::in_place,
|
std::invoke(std::forward<F>(f), error())
|
| (3,4) | G(std::in_place, std::move(
|
std::invoke(std::forward<F>(f), std::move(error()))
|
| (5,6) | G()
|
std::invoke(std::forward<F>(f), error())
|
| (7,8) | std::invoke(std::forward<F>(f), std::move(error()))
| |
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_expected |
202211L |
(C++23) | Monadic functions for std::expected
|
Example
Reason: no example
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3938 | C++23 | the expected value was obtained by value()[1]
|
changed to **this
|
| LWG 3973 | C++23 | the expected value was obtained by **this[2]
|
changed to
|
- ↑
value()requiresEto be copy constructible (see LWG issue 3843), whereoperator*does not. - ↑
**thiscan trigger argument-dependent lookup.
See also
returns the expected itself if it contains an expected value; otherwise, returns an expected containing the transformed unexpected value (public member function) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.