std::expected<T,E>::value_or

< cpp‎ | utility‎ | expected
主模板
template< class U >
constexpr T value_or( U&& default_value ) const&;
(1) (C++23 起)
template< class U >
constexpr T value_or( U&& default_value ) &&;
(2) (C++23 起)

*this 包含预期值时返回它,否则返回 default_value

void 部分特化没有这些成员函数。

1) 如果 std::is_copy_constructible_v<T>std::is_convertible_v<U, T>false,那么程序非良构。
2) 如果 std::is_move_constructible_v<T>std::is_convertible_v<U, T>false,那么程序非良构。

参数

default_value - *this 不包含预期值的情况下使用的值

返回值

1) has_value() ? **this : static_cast<T>(std::forward<U>(default_value))
2) has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(default_value))

示例

参阅

返回预期值
(公开成员函数)