class template
std::expected(C++23)
namespace std {
template<class T, class E>
class expected;
// T=cv void 部分特殊化
template<class T, class E>
requires is_void_v<T>
class expected<T, E>;
}
概要
expected
クラスは、任意の型T
の値を正常値とし任意の型E
の値をエラー値として、正常もしくはエラーいずれかの状態を取ることを値として表現できる型である。
- 型
T
が(CV修飾された)void
型でなければ、型T
はCpp17Destructible要件を満たすこと。
- 型
E
はCpp17Destructible要件を満たすこと。
メンバ関数
構築・破棄
代入
名前 |
説明 |
対応バージョン |
operator= |
代入演算子 |
C++23 |
emplace |
正常値型のコンストラクタ引数から直接構築する |
C++23 |
swap |
他のexpected オブジェクトとデータを入れ替える |
C++23 |
値の観測
名前 |
説明 |
対応バージョン |
operator-> |
メンバアクセス |
C++23 |
operator* |
間接参照 |
C++23 |
operator bool |
正常値を保持しているかを判定する |
C++23 |
has_value |
正常値を保持しているかを判定する |
C++23 |
value |
正常値を取得する |
C++23 |
error |
エラー値を取得する |
C++23 |
value_or |
正常値もしくは指定された値を取得する |
C++23 |
モナド操作
名前 |
説明 |
対応バージョン |
and_then |
正常値に対して関数を適用する |
C++23 |
or_else |
エラー値に対して関数を適用する |
C++23 |
transform |
正常値を変換する |
C++23 |
transform_error |
エラー値を変換する |
C++23 |
比較
名前 |
説明 |
対応バージョン |
operator== |
等値比較 |
C++23 |
operator!= |
非等値比較 |
C++23 |
メンバ型
名前 |
説明 |
対応バージョン |
value_type |
正常値の型T |
C++23 |
error_type |
エラー値の型E |
C++23 |
unexpected_type |
unexpected<E> |
C++23 |
template<class U> rebind |
expected<U, error_type> |
C++23 |
例
出力
5
"out of domain"
"divide by zero"
バージョン
言語
処理系
関連項目
参照