template< class U > constexpr T value_or( U&& default_value ) const&; | (1) | (since C++23) |
template< class U > constexpr T value_or( U&& default_value ) &&; | (2) | (since C++23) |
Returns the contained value if *this
contains an expected value, otherwise returns default_value
.
bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value))
bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value))
default_value | - | the value to use in case *this does not contain an expected value |
Type requirements | ||
-T must meet the requirements of CopyConstructible in order to use overload (1). |
||
-T must meet the requirements of MoveConstructible in order to use overload (2). |
||
-U&& must be convertible to T |
The currently contained value if *this
contains an expected value, or default_value
otherwise.
Any exception thrown by the selected constructor of the return value T
.
If T
is (possibly cv-qualified) void
, this member is not declared.
(C++23) | returns the expected value (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/expected/value_or