From da6efe1a08463c917c6d8dfa6fb453612c98d055 Mon Sep 17 00:00:00 2001 From: yoh Date: Fri, 10 Feb 2023 14:29:25 +0900 Subject: [PATCH] expected: bad_expected_access(#1066) --- reference/expected/bad_expected_access.md | 8 +-- .../expected/bad_expected_access/error.md | 71 +++++++++++++++++++ .../bad_expected_access/op_constructor.md | 52 ++++++++++++++ .../expected/bad_expected_access/what.md | 64 +++++++++++++++++ reference/expected/expected.void/value.md | 2 +- reference/expected/expected/value.md | 2 +- reference/expected/unexpected.md | 4 +- reference/expected/unexpected/error.md | 4 +- .../expected/unexpected/op_constructor.md | 8 +-- .../expected/unexpected/op_deduction_guide.md | 2 +- 10 files changed, 202 insertions(+), 15 deletions(-) create mode 100644 reference/expected/bad_expected_access/error.md create mode 100644 reference/expected/bad_expected_access/op_constructor.md create mode 100644 reference/expected/bad_expected_access/what.md diff --git a/reference/expected/bad_expected_access.md b/reference/expected/bad_expected_access.md index b4ec99ce13..1f18724c7e 100644 --- a/reference/expected/bad_expected_access.md +++ b/reference/expected/bad_expected_access.md @@ -9,7 +9,6 @@ namespace std { template class bad_expected_access : public bad_expected_access { ... }; - // void特殊化 template<> class bad_expected_access : public exception { ... }; } @@ -24,10 +23,11 @@ namespace std { | 名前 | 説明 | 対応バージョン | |-----------------|----------------|-------| -| [`(constructor)`](bad_expected_access/op_constructor.md.nolink) | コンストラクタ | C++23 | +| [`(constructor)`](bad_expected_access/op_constructor.md) | コンストラクタ | C++23 | | `(destructor)` | デストラクタ | C++23 | -| [`error`](bad_expected_access/error.md.nolink) | エラー値を取得する | C++23 | -| [`what`](bad_expected_access/what.md.nolink) | エラー理由の文字列を取得する | C++23 | +| `operator=` | コピー/ムーブ代入演算子 | C++23 | +| [`error`](bad_expected_access/error.md) | エラー値を取得する | C++23 | +| [`what`](bad_expected_access/what.md) | エラー理由の文字列を取得する | C++23 | ## 例 diff --git a/reference/expected/bad_expected_access/error.md b/reference/expected/bad_expected_access/error.md new file mode 100644 index 0000000000..66947a8e76 --- /dev/null +++ b/reference/expected/bad_expected_access/error.md @@ -0,0 +1,71 @@ +# error +* expected[meta header] +* function[meta id-type] +* std[meta namespace] +* bad_expected_access[meta class] +* cpp23[meta cpp] + +```cpp +constexpr const E& error() const & noexcept; // (1) +constexpr E& error() & noexcept; // (2) +constexpr const E&& error() const && noexcept; // (3) +constexpr E&& error() && noexcept; // (4) +``` + +## 概要 +エラー値を取得する。 + + +## 戻り値 +動作説明用のメンバ変数として、エラー値を保持する`unex`を導入する。 + +- (1), (2) : `unex` +- (3), (4) : [`std::move`](/reference/utility/move.md)`(unex)` + + +## 例外 +投げない + + +## 例 +```cpp example +#include +#include +#include +#include + +int main() +{ + std::expected v = std::unexpected{"ERR"}; + try { + std::cout << v.value() << std::endl; + } catch (const std::bad_expected_access& 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](/implementation.md#clang): 16.0 +- [GCC](/implementation.md#gcc): 12.1 +- [ICC](/implementation.md#icc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? + + +## 参照 +- [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) diff --git a/reference/expected/bad_expected_access/op_constructor.md b/reference/expected/bad_expected_access/op_constructor.md new file mode 100644 index 0000000000..4049783bdd --- /dev/null +++ b/reference/expected/bad_expected_access/op_constructor.md @@ -0,0 +1,52 @@ +# コンストラクタ +* expected[meta header] +* function[meta id-type] +* std[meta namespace] +* bad_expected_access[meta class] +* cpp23[meta cpp] + +```cpp +explicit bad_expected_access(E e); // (1) +bad_expected_access(const bad_expected_access&); // (2) +bad_expected_access(bad_expected_access&&); // (3) +``` +* bad_expected_access[link ../bad_expected_access.md] + +## 概要 +- (1) : エラー値を[`std::move`](/reference/utility/move.md)`(e)`で初期化する。 +- (2) : コピーコンストラクタ。 +- (3) : ムーブコンストラクタ。 + + +## 例 +```cpp example +#include +#include + +int main() +{ + std::bad_expected_access ex{42}; + assert(ex.error() == 42); +} +``` +* std::bad_expected_access[color ff0000] +* error()[link error.md] + +### 出力 +``` +``` + + +## バージョン +### 言語 +- C++23 + +### 処理系 +- [Clang](/implementation.md#clang): 16.0 +- [GCC](/implementation.md#gcc): 12.1 +- [ICC](/implementation.md#icc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? + + +## 参照 +- [P0323R12 std::expected](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html) diff --git a/reference/expected/bad_expected_access/what.md b/reference/expected/bad_expected_access/what.md new file mode 100644 index 0000000000..38ad29ef36 --- /dev/null +++ b/reference/expected/bad_expected_access/what.md @@ -0,0 +1,64 @@ +# what +* expected[meta header] +* function[meta id-type] +* std[meta namespace] +* bad_expected_access[meta class] +* cpp23[meta cpp] + +```cpp +const char* what() const noexcept override; +``` + +## 概要 +エラー理由の文字列を取得する。 + + +## 戻り値 +エラー理由となる実装定義の文字列 + + +## 例外 +投げない + + +## 例 +```cpp example +#include +#include +#include +#include + +int main() +{ + std::expected v = std::unexpected{"ERR"}; + try { + std::cout << v.value() << std::endl; + } catch (const std::bad_expected_access& ex) { + std::cout << ex.what() << std::endl; + } +} +``` +* what()[color ff0000] +* value()[link ../expected/value.md] +* std::unexpected[link ../unexpected.md] +* std::bad_expected_access[link ../bad_expected_access.md] + +### 出力例 +``` +bad access to std::expected without expected value +``` + + +## バージョン +### 言語 +- C++23 + +### 処理系 +- [Clang](/implementation.md#clang): 16.0 +- [GCC](/implementation.md#gcc): 12.1 +- [ICC](/implementation.md#icc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? + + +## 参照 +- [P0323R12 std::expected](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0323r12.html) diff --git a/reference/expected/expected.void/value.md b/reference/expected/expected.void/value.md index 45e4c49db0..a69e3d1d17 100644 --- a/reference/expected/expected.void/value.md +++ b/reference/expected/expected.void/value.md @@ -43,7 +43,7 @@ int main() } ``` * value()[color ff0000] -* error()[link ../bad_expected_access/error.md.nolink] +* error()[link ../bad_expected_access/error.md] * std::unexpected[link ../unexpected.md] * std::bad_expected_access[link ../bad_expected_access.md] diff --git a/reference/expected/expected/value.md b/reference/expected/expected/value.md index 8f1f26ffca..ab4a91004c 100644 --- a/reference/expected/expected/value.md +++ b/reference/expected/expected/value.md @@ -48,7 +48,7 @@ int main() } ``` * value()[color ff0000] -* error()[link ../bad_expected_access/error.md.nolink] +* error()[link ../bad_expected_access/error.md] * std::unexpected[link ../unexpected.md] * std::bad_expected_access[link ../bad_expected_access.md] diff --git a/reference/expected/unexpected.md b/reference/expected/unexpected.md index 0ce15ea9cf..e0dc8715d3 100644 --- a/reference/expected/unexpected.md +++ b/reference/expected/unexpected.md @@ -12,7 +12,7 @@ namespace std { ``` ## 概要 -`unexpected`クラスは、[`std::expected`](expected.md)に格納される任意の型`E`の値をエラー値として表現する。 +`unexpected`クラスは、[`std::expected`](expected.md)に格納される任意の型`E`のエラー値を表現するヘルパークラスである。 ## 適格要件 @@ -39,7 +39,7 @@ namespace std { | 名前 | 説明 | 対応バージョン | |-----------------|----------------|-------| -| [`error`](unexpected/error.md) | 値を取得する | C++23 | +| [`error`](unexpected/error.md) | エラー値を取得する | C++23 | ### 比較 diff --git a/reference/expected/unexpected/error.md b/reference/expected/unexpected/error.md index 054b65f7b3..8a6d6922bf 100644 --- a/reference/expected/unexpected/error.md +++ b/reference/expected/unexpected/error.md @@ -13,11 +13,11 @@ constexpr E&& error() && noexcept; // (4) ``` ## 概要 -値を取得する。 +エラー値を取得する。 ## 戻り値 -動作説明用のメンバ変数として、値を保持する`unex`を導入する。 +動作説明用のメンバ変数として、エラー値を保持する`unex`を導入する。 - (1), (2) : `unex` - (3), (4) : [`std::move`](/reference/utility/move.md)`(unex)` diff --git a/reference/expected/unexpected/op_constructor.md b/reference/expected/unexpected/op_constructor.md index 258bd17dc9..763cccae60 100644 --- a/reference/expected/unexpected/op_constructor.md +++ b/reference/expected/unexpected/op_constructor.md @@ -38,9 +38,9 @@ constexpr explicit unexpected(in_place_t, initializer_list il, Args&&... args ## 効果 -- (3) : [`std::forward`](/reference/utility/forward.md)`(e)`で値を直接非リスト初期化する。 -- (4) : [`std::forward`](/reference/utility/forward.md)`(args)...`で値を直接非リスト初期化する。 -- (5) : `il,` [`std::forward`](/reference/utility/forward.md)`(args)...`で値を直接非リスト初期化する。 +- (3) : [`std::forward`](/reference/utility/forward.md)`(e)`でエラー値を直接非リスト初期化する。 +- (4) : [`std::forward`](/reference/utility/forward.md)`(args)...`でエラー値を直接非リスト初期化する。 +- (5) : `il,` [`std::forward`](/reference/utility/forward.md)`(args)...`でエラー値を直接非リスト初期化する。 ## 例外 @@ -100,7 +100,7 @@ int main() std::unexpected dst{src}; assert((dst.error() == IntTuple{1, 2})); } - // (3) 変換構築 + // (3) 変換ムーブ構築 { UniquePtr src = std::make_unique(42); std::unexpected dst{std::move(src)}; diff --git a/reference/expected/unexpected/op_deduction_guide.md b/reference/expected/unexpected/op_deduction_guide.md index dd79e8017a..0927d9d581 100644 --- a/reference/expected/unexpected/op_deduction_guide.md +++ b/reference/expected/unexpected/op_deduction_guide.md @@ -12,7 +12,7 @@ namespace std { ``` ## 概要 -`std::expected`クラステンプレートの型推論補助。 +`std::unexpected`クラステンプレートの型推論補助。 ## 例