- expected[meta header]
- class template[meta id-type]
- std[meta namespace]
- cpp23[meta cpp]
namespace std {
template<class E>
class bad_expected_access : public bad_expected_access<void> { ... };
template<>
class bad_expected_access<void> : public exception { ... };
}
- exception[link /reference/exception/exception.md]
bad_expected_access
は、expected
オブジェクトがエラー値を保持しているとき正常値にアクセスした場合に発生する例外である。
名前 | 説明 | 対応バージョン |
---|---|---|
(constructor) |
コンストラクタ | C++23 |
(destructor) |
デストラクタ | C++23 |
operator= |
コピー/ムーブ代入演算子 | C++23 |
error |
エラー値を取得する | C++23 |
what |
エラー理由の文字列を取得する | C++23 |
#include <cassert>
#include <expected>
#include <iostream>
#include <string>
int main()
{
std::expected<int, std::string> x = std::unexpected{"invalid"};
try {
assert(not x.has_value());
int value = x.value(); // bad_expected_access例外発生
std::cout << "value: " << value << std::endl;
}
catch (const std::bad_expected_access<std::string>& ex) {
std::cout << "error: " << ex.error() << std::endl;
}
}
- std::bad_expected_access[color ff0000]
- std::unexpected[link unexpected.md]
error: invalid
- C++23
- Clang: 16.0 [mark verified]
- GCC: 12.1 [mark verified]
- ICC: ??
- Visual C++: ??