diff --git a/GLOBAL_QUALIFY_LIST.txt b/GLOBAL_QUALIFY_LIST.txt index 2bb6b88df2..3c202f133a 100644 --- a/GLOBAL_QUALIFY_LIST.txt +++ b/GLOBAL_QUALIFY_LIST.txt @@ -90,6 +90,8 @@ * std::execution::par[link /reference/execution/execution/execution_policy.md] * std::execution::par_unseq[link /reference/execution/execution/execution_policy.md] * std::execution::seq[link /reference/execution/execution/execution_policy.md] +* [link /reference/expected.md] + * std::expected[link /reference/expected/expected.md] * [link /reference/filesystem.md] * fe.what()[link /reference/filesystem/filesystem_error/what.md] * fs::filesystem_error[link /reference/filesystem/filesystem_error.md] diff --git a/lang/cpp23.md b/lang/cpp23.md index 2263b8a0a8..f5f33237d5 100644 --- a/lang/cpp23.md +++ b/lang/cpp23.md @@ -115,7 +115,7 @@ C++23とは、2023年中に改訂される予定の、C++バージョンの通 - スタックトレースを取得するためのライブラリとして[``](/reference/stacktrace.md)を追加 - CとC++の間でのアトミック操作の相互運用のため、C互換ライブラリとして[``](/reference/stdatomic.h.md)を追加 - 外部から提供されるメモリバッファでストリーム処理を行うライブラリとして[``](/reference/spanstream.md.nolink)を追加 -- 正常値とエラー値のどちらかを持つクラスおよびライブラリとして[``](/reference/expected.md.nolink)を追加 +- 正常値とエラー値のどちらかを持つクラスおよびライブラリとして[``](/reference/expected.md)を追加 - 多次元配列ビューのライブラリとして[``](/reference/mdspan.md.nolink)を追加 - ノードベースではないソート済みキーによる順序付き連想コンテナのライブラリとして、[``](/reference/flat_map.md.nolink)と[``](/reference/flat_set.md.nolink)を追加 - 書式指定で出力するライブラリとして[``](/reference/print.md.nolink)を追加 diff --git a/reference.md b/reference.md index e43685df13..82516c2871 100644 --- a/reference.md +++ b/reference.md @@ -63,7 +63,7 @@ | [``](/reference/optional.md) | 任意で値を持たせられるオブジェクト | C++17 | | [``](/reference/variant.md) | 候補の型を切り替えながら保持できる記憶域型 | C++17 | | [``](/reference/any.md) | あらゆる型の値を保持できる記憶域型 | C++17 | -| [``](/reference/expected.md.nolink) | 正常値かエラー値のどちらかを持たせられるオブジェクト | C++23 | +| [``](/reference/expected.md) | 正常値かエラー値のどちらかを持たせられるオブジェクト | C++23 | | [``](/reference/type_traits.md) | 型特性 | C++11 | | [``](/reference/functional.md) | 関数オブジェクト | | | [``](/reference/memory.md) | メモリ | | diff --git a/reference/expected.md b/reference/expected.md new file mode 100644 index 0000000000..3794925f44 --- /dev/null +++ b/reference/expected.md @@ -0,0 +1,34 @@ +# expected +* expected[meta header] +* cpp23[meta cpp] + +``ヘッダでは、任意の正常値または任意のエラー値のどちらかを持たせられるオブジェクトの型を定義する。 + + +| 名前 | 説明 | 対応バージョン | +|-----------------|----------------|-------| +| [`expected`](expected/expected.md) | 正常値かエラー値を保持するオブジェクト (class template) | C++23 | +| [`unexpected`](expected/unexpected.md.nolink) | エラー値の代入補助クラス (class template) | C++23 | +| [`unexpect_t`](expected/unexpect_t.md.nolink) | エラー値の直接構築を指示するタグ型 (class) | C++23 | +| [`bad_expected_access`](expected/bad_expected_access.md.nolink) | エラー値保持時に正常値へアクセスした場合に発生する例外 (class template) | C++23 | + + +## バージョン +### 言語 +- C++23 + +### 処理系 +- [Clang](/implementation.md#clang): 16.0 +- [GCC](/implementation.md#gcc): 12.1 +- [ICC](/implementation.md#icc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? + + +## 関連項目 +- [`optional`](optional.md) + + +## 参照 +- [P0323R12 std::expected](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html) +- [P2549R1 `std::unexpected` should have `error()` as member accessor](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2549r1.html) +- [P2505R5 Monadic Functions for `std::expected`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2505r5.html) diff --git a/reference/expected/expected.md b/reference/expected/expected.md new file mode 100644 index 0000000000..34e775ca7d --- /dev/null +++ b/reference/expected/expected.md @@ -0,0 +1,120 @@ +# expected +* expected[meta header] +* class template[meta id-type] +* std[meta namespace] +* cpp23[meta cpp] + +```cpp +namespace std { + template + class expected; + + // T=cv void 部分特殊化 + template + requires is_void_v + class expected; +} +``` + +## 概要 +`expected`クラスは、任意の型`T`の値を正常値とし任意の型`E`の値をエラー値として、正常もしくはエラーいずれかの状態を取ることを値として表現できる型である。 + + +## 適格要件 + +- 型`T`が(CV修飾された)`void`型でなければ、型`T`はCpp17Destructible要件を満たすこと。 +- 型`E`はCpp17Destructible要件を満たすこと。 + + +## メンバ関数 +### 構築・破棄 + +| 名前 | 説明 | 対応バージョン | +|-----------------|----------------|-------| +| [`(constructor)`](expected/op_constructor.md.nolink) | コンストラクタ | C++23 | +| [`(destructor)`](expected/op_destructor.md.nolink) | デストラクタ | C++23 | + +### 代入 + +| 名前 | 説明 | 対応バージョン | +|-----------------|----------------|-------| +| [`operator=`](expected/op_assign.md.nolink) | 代入演算子 | C++23 | +| [`emplace`](expected/emplace.md.nolink) | 正常値型のコンストラクタ引数から直接構築する | C++23 | +| [`swap`](expected/swap.md.nolink) | 他の`expected`オブジェクトとデータを入れ替える | C++23 | + +### 値の観測 + +| 名前 | 説明 | 対応バージョン | +|-----------------|----------------|-------| +| [`operator->`](expected/op_arrow.md.nolink) | メンバアクセス | C++23 | +| [`operator*`](expected/op_deref.md.nolink) | 間接参照 | C++23 | +| [`operator bool`](expected/op_bool.md.nolink) | 正常値を保持しているかを判定する | C++23 | +| [`has_value`](expected/has_value.md.nolink) | 正常値を保持しているかを判定する | C++23 | +| [`value`](expected/value.md.nolink) | 正常値を取得する | C++23 | +| [`error`](expected/error.md.nolink) | エラー値を取得する | C++23 | +| [`value_or`](expected/value_or.md.nolink) | 正常値もしくは指定された値を取得する | C++23 | + +### モナド操作 + +| 名前 | 説明 | 対応バージョン | +|------|------|----------------| +| [`and_then`](expected/and_then.md.nolink) | 正常値に対して関数を適用する | C++23 | +| [`or_else`](expected/or_else.md.nolink) | エラー値に対して関数を適用する | C++23 | +| [`transform`](expected/transform.md.nolink) | 正常値を変換する | C++23 | +| [`transform_error`](expected/transform_error.md.nolink) | エラー値を変換する | C++23 | + +### 比較 + +| 名前 | 説明 | 対応バージョン | +|--------------|------------|-------| +| `operator==` | 等値比較 | C++23 | +| `operator!=` | 非等値比較 | C++23 | + + +## メンバ型 + +| 名前 | 説明 | 対応バージョン | +|-------------------|-----------------|-------| +| `value_type` | 正常値の型`T` | C++23 | +| `error_type` | エラー値の型`E` | C++23 | +| `unexpected_type` | [`unexpected`](unexpected.md.nolink) | C++23 | +| `template rebind` | `expected` | C++23 | + + +## 例 +```cpp example +#include +#include + +int main() +{ + int variable = 0; + std::cout << variable << std::endl; +} +``` +* std::expected[color ff0000] + +### 出力 +``` +0 +``` + + +## バージョン +### 言語 +- C++23 + +### 処理系 +- [Clang](/implementation.md#clang): 16.0 +- [GCC](/implementation.md#gcc): 12.1 +- [ICC](/implementation.md#icc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? + + +## 関連項目 +- [`optional`](/reference/optional/optional.md) + + +## 参照 +- [P0323R12 std::expected](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html) +- [P2505R5 Monadic Functions for `std::expected`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2505r5.html)