Skip to content

Commit

Permalink
expected: bad_expected_access<E>(#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohhoy committed Feb 10, 2023
1 parent 141e98e commit da6efe1
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 15 deletions.
8 changes: 4 additions & 4 deletions reference/expected/bad_expected_access.md
Expand Up @@ -9,7 +9,6 @@ namespace std {
template<class E>
class bad_expected_access : public bad_expected_access<void> { ... };

// void特殊化
template<>
class bad_expected_access<void> : public exception { ... };
}
Expand All @@ -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 |
## 例
Expand Down
71 changes: 71 additions & 0 deletions 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 <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](/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<E>` should have `error()` as member accessor](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2549r1.html)
52 changes: 52 additions & 0 deletions 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 <cassert>
#include <expected>
int main()
{
std::bad_expected_access<int> 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)
64 changes: 64 additions & 0 deletions 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 <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 << 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)
2 changes: 1 addition & 1 deletion reference/expected/expected.void/value.md
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion reference/expected/expected/value.md
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions reference/expected/unexpected.md
Expand Up @@ -12,7 +12,7 @@ namespace std {
```
## 概要
`unexpected`クラスは、[`std::expected<T, E>`](expected.md)に格納される任意の型`E`の値をエラー値として表現する
`unexpected`クラスは、[`std::expected<T, E>`](expected.md)に格納される任意の型`E`のエラー値を表現するヘルパークラスである
## 適格要件
Expand All @@ -39,7 +39,7 @@ namespace std {
| 名前 | 説明 | 対応バージョン |
|-----------------|----------------|-------|
| [`error`](unexpected/error.md) | 値を取得する | C++23 |
| [`error`](unexpected/error.md) | エラー値を取得する | C++23 |
### 比較
Expand Down
4 changes: 2 additions & 2 deletions reference/expected/unexpected/error.md
Expand Up @@ -13,11 +13,11 @@ constexpr E&& error() && noexcept; // (4)
```

## 概要
値を取得する
エラー値を取得する


## 戻り値
動作説明用のメンバ変数として、値を保持する`unex`を導入する。
動作説明用のメンバ変数として、エラー値を保持する`unex`を導入する。

- (1), (2) : `unex`
- (3), (4) : [`std::move`](/reference/utility/move.md)`(unex)`
Expand Down
8 changes: 4 additions & 4 deletions reference/expected/unexpected/op_constructor.md
Expand Up @@ -38,9 +38,9 @@ constexpr explicit unexpected(in_place_t, initializer_list<U> il, Args&&... args
## 効果
- (3) : [`std::forward`](/reference/utility/forward.md)`<Err>(e)`で値を直接非リスト初期化する
- (4) : [`std::forward`](/reference/utility/forward.md)`<Args>(args)...`で値を直接非リスト初期化する
- (5) : `il,` [`std::forward`](/reference/utility/forward.md)`<Args>(args)...`で値を直接非リスト初期化する
- (3) : [`std::forward`](/reference/utility/forward.md)`<Err>(e)`でエラー値を直接非リスト初期化する
- (4) : [`std::forward`](/reference/utility/forward.md)`<Args>(args)...`でエラー値を直接非リスト初期化する
- (5) : `il,` [`std::forward`](/reference/utility/forward.md)`<Args>(args)...`でエラー値を直接非リスト初期化する
## 例外
Expand Down Expand Up @@ -100,7 +100,7 @@ int main()
std::unexpected<IntTuple> dst{src};
assert((dst.error() == IntTuple{1, 2}));
}
// (3) 変換構築
// (3) 変換ムーブ構築
{
UniquePtr src = std::make_unique<int>(42);
std::unexpected<SharedPtr> dst{std::move(src)};
Expand Down
2 changes: 1 addition & 1 deletion reference/expected/unexpected/op_deduction_guide.md
Expand Up @@ -12,7 +12,7 @@ namespace std {
```
## 概要
`std::expected`クラステンプレートの型推論補助。
`std::unexpected`クラステンプレートの型推論補助。
## 例
Expand Down

0 comments on commit da6efe1

Please sign in to comment.