// expected<cv void, E>部分特殊化
constexpr void value() const &; // (1)
constexpr void value() &&; // (2)
概要
正常値(void)を取得する。
適格要件
- (1) :
is_copy_constructible_v<E> == trueであること - (2) :
is_copy_constructible_v<E> == trueかつis_move_constructible_v<E> == trueであること
戻り値
なし
備考
この関数は、例外を送出しうるため、フリースタンディング処理系では削除される(フリースタンディング処理系では使用できない)。
例外
- (1) : エラー値を保持していたら、例外
bad_expected_access(error())を送出する - (2) : エラー値を保持していたら、例外
bad_expected_access(std::move(error()))を送出する
例
#include <expected>
#include <iostream>
int main()
{
std::expected<void, int> x;
x.value();
std::expected<void, int> y = std::unexpected{42};
try {
y.value();
} catch (const std::bad_expected_access<int>& ex) {
std::cout << "throw:" << ex.error() << std::endl;
}
}
出力
throw:42
バージョン
言語
- C++23
処理系
- Clang: 16.0 ✅
- GCC: 12.1 ✅
- ICC: ??
- Visual C++: ??
関連項目
参照
- P0323R12 std::expected
- LWG Issue 3940.
std::expected<void, E>::value()also needsEto be copy constructible- C++26で、(1)に
Eがコピー構築可能であること、(2)にEがコピー構築可能かつムーブ構築可能であることを要求する適格要件が追加された
- C++26で、(1)に