- expected[meta header]
- function[meta id-type]
- std[meta namespace]
- bad_expected_access[meta class]
- cpp23[meta cpp]
const E& error() const & noexcept; // (1) C++23
constexpr const E& error() const & noexcept; // (1) C++26
E& error() & noexcept; // (2) C++23
constexpr E& error() & noexcept; // (2) C++26
const E&& error() const && noexcept; // (3) C++23
constexpr const E&& error() const && noexcept; // (3) C++26
E&& error() && noexcept; // (4) C++23
constexpr E&& error() && noexcept; // (4) C++26
エラー値を取得する。
動作説明用のメンバ変数として、エラー値を保持するunex
を導入する。
- (1), (2) :
unex
- (3), (4) :
std::move
(unex)
投げない
#include <cassert>
#include <expected>
#include <iostream>
#include <string>
int main()
{
std::expected<int, std::string> v = std::unexpected{"ERR"};
try {
std::cout << v.value() << std::endl;
} catch (const std::bad_expected_access<std::string>& ex) {
std::cout << "throw:" << ex.error() << std::endl;
}
}
- error()[color ff0000]
- value()[link ../expected/value.md]
- std::unexpected[link ../unexpected.md]
- std::bad_expected_access[link ../bad_expected_access.md]
throw:ERR
- C++23
- Clang: 16.0 [mark verified]
- GCC: 12.1 [mark verified]
- ICC: ??
- Visual C++: ??