Skip to content

Commit

Permalink
expected/expected: operator==,!=(#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohhoy committed Feb 5, 2023
1 parent c7fe7e2 commit 4d92044
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 2 deletions.
4 changes: 2 additions & 2 deletions reference/expected/expected.md
Expand Up @@ -69,8 +69,8 @@ namespace std {
| 名前 | 説明 | 対応バージョン |
|--------------|------------|-------|
| [`operator==`](unexpected/op_equal.md.nolink) | 等値比較 | C++23 |
| [`operator!=`](unexpected/op_not_equal.md.nolink) | 非等値比較 | C++23 |
| [`operator==`](expected/op_equal.md) | 等値比較 | C++23 |
| [`operator!=`](expected/op_not_equal.md) | 非等値比較 | C++23 |
## メンバ型
Expand Down
95 changes: 95 additions & 0 deletions reference/expected/expected/op_equal.md
@@ -0,0 +1,95 @@
# operator==
* expected[meta header]
* function template[meta id-type]
* std[meta namespace]
* expected[meta class]
* cpp23[meta cpp]

```cpp
template<class T2, class E2> requires (!is_void_v<T2>)
friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y); // (1)

template<class T2>
friend constexpr bool operator==(const expected& x, const T2& v); // (2)
// (2)により、下記オーバーロードが使用可能になる
template<class T2>
friend constexpr bool operator==(const T2& v, const expected& x); // (3)

template<class E2>
friend constexpr bool operator==(const expected& x, const unexpected<E2>& e); // (4)
// (4)により、下記オーバーロードが使用可能になる
template<class E2>
friend constexpr bool operator==(const unexpected<E2>& e, const expected& x); // (5)
```
* is_void_v[link /reference/type_traits/is_void.md]
* unexpected[link ../unexpected.md]
## 概要
- (1) : `unexpected`オブジェクト同士の等値比較を行う。
- (2), (3) : `unexpected`オブジェクトと正常値の等値比較を行う。
- (4), (5) : `unexpected`オブジェクトとエラー値の等値比較を行う。
## 適格要件
- (1) : 式[`*x`](op_deref.md) `==` [`*y`](op_deref.md)および式`x.`[`error()`](error.md) `== y.`[`error()`](error.md)が適格であり、各式の結果を`bool`へ変換可能であること。
- (2), (3) : 式[`*x`](op_deref.md) `== v`が適格であり、その結果を`bool`へ変換可能であること。
- (4), (5) : 式`x.`[`error()`](error.md) `== e.`[`error()`](../unexpected/error.md.nolink)が適格であり、その結果を`bool`へ変換可能であること。
## 戻り値
- (1) : 次の値を返す
- `x.`[`has_value()`](has_value.md)と`y.`[`has_value()`](has_value.md)が異なるとき、`false`
- `x.`[`has_value()`](has_value.md) `== true`のとき、[`*x`](op_deref.md) `==` [`*y`](op_deref.md)
- `x.`[`error()`](error.md) `== y.`[`error()`](error.md)
- (2), (3) : `x.`[`has_value()`](has_value.md) `&& static_cast<bool>(`[`*x`](op_deref.md) `== v)`
- (4), (5) : `!x.`[`has_value()`](has_value.md) `&& static_cast<bool>(x.`[`error()`](error.md) `== e.`[`error()`](../unexpected/error.md.nolink)`)`
## 例
```cpp example
#include <cassert>
#include <expected>
int main()
{
std::expected<long, long> x1 = 1;
std::expected<short, short> y1 = 1;
std::expected<long, long> x2 = std::unexpected{1};
std::expected<short, short> y2 = std::unexpected{1};
// (1)
assert(x1 == y1);
assert(x2 == y2);
assert(not (x1 == y2));
assert(not (x2 == y1));
// (2), (3)
assert(x1 == 1);
assert(1 == x1);
// (4), (5)
assert(x2 == std::unexpected{1});
assert(std::unexpected{1} == x2);
}
```
* ==[color ff0000]
* std::unexpected[link ../unexpected.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)
92 changes: 92 additions & 0 deletions reference/expected/expected/op_not_equal.md
@@ -0,0 +1,92 @@
# operator!=
* expected[meta header]
* function template[meta id-type]
* std[meta namespace]
* expected[meta class]
* cpp23[meta cpp]

```cpp
// operator==により、下記オーバーロードが使用可能になる
template<class T2, class E2> requires (!is_void_v<T2>)
friend constexpr bool operator!=(const expected& x, const expected<T2, E2>& y); // (1)

template<class T2>
friend constexpr bool operator!=(const expected& x, const T2& v); // (2)
template<class T2>
friend constexpr bool operator!=(const T2& v, const expected& x); // (3)

template<class E2>
friend constexpr bool operator!=(const expected& x, const unexpected<E2>& e); // (4)
template<class E2>
friend constexpr bool operator!=(const unexpected<E2>& e, const expected& x); // (5)
```
* operator==[link op_equal.md]
* is_void_v[link /reference/type_traits/is_void.md]
* unexpected[link ../unexpected.md]
## 概要
- (1) : `unexpected`オブジェクト同士の非等値比較を行う。
- (2), (3) : `unexpected`オブジェクトと正常値の非等値比較を行う。
- (4), (5) : `unexpected`オブジェクトとエラー値の非等値比較を行う。
## 適格要件
- (1) : 式[`*x`](op_deref.md) `==` [`*y`](op_deref.md)および式`x.`[`error()`](error.md) `== y.`[`error()`](error.md)が適格であり、各式の結果を`bool`へ変換可能であること。
- (2), (3) : 式[`*x`](op_deref.md) `== v`が適格であり、その結果を`bool`へ変換可能であること。
- (4), (5) : 式`x.`[`error()`](error.md) `== e.`[`error()`](../unexpected/error.md.nolink)が適格であり、その結果を`bool`へ変換可能であること。
## 戻り値
- (1) : `!`[`(x == y)`](op_equal.md)
- (2), (3) : `!`[`(x == v)`](op_equal.md)
- (4), (5) : `!`[`(x == e)`](op_equal.md)
## 例
```cpp example
#include <cassert>
#include <expected>
int main()
{
std::expected<long, long> x1 = 1;
std::expected<short, short> y1 = 100;
std::expected<long, long> x2 = std::unexpected{1};
std::expected<short, short> y2 = std::unexpected{100};
// (1)
assert(x1 != y1);
assert(x2 != y2);
assert(x1 != y2);
assert(x2 != y1);
// (2), (3)
assert(x1 != 2);
assert(2 != x1);
// (4), (5)
assert(x2 != std::unexpected{2});
assert(std::unexpected{2} != x2);
}
```
* !=[color ff0000]
* std::unexpected[link ../unexpected.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)

0 comments on commit 4d92044

Please sign in to comment.