- expected[meta header]
- function template[meta id-type]
- std[meta namespace]
- expected[meta class]
- cpp23[meta cpp]
template<class U> constexpr T value_or(U&& v) const &; // (1)
template<class U> constexpr T value_or(U&& v) &&; // (2)
正常値もしくは指定された値を取得する。
- (1) :
is_copy_constructible_v
<T> == true &&
is_convertible_v
<U, T> == true
- (2) :
is_move_constructible_v
<T> == true &&
is_convertible_v
<U, T> == true
- (1) :
has_value()
?
**this
: static_cast<T>(
std::forward
<U>(v))
- (2) :
has_value()
?
std::move
(
**this
) : static_cast<T>(
std::forward
<U>(v))
#include <expected>
#include <iostream>
#include <string>
int main()
{
std::expected<int, std::string> x = 42;
std::cout << x.value_or(0) << std::endl;
std::expected<int, std::string> y = std::unexpected{"ERR"};
std::cout << y.value_or(0) << std::endl;
}
- value_or()[color ff0000]
- std::unexpected[link ../unexpected.md]
42
0
- C++23
- Clang: 16.0
- GCC: 12.1
- ICC: ??
- Visual C++: ??