Skip to content

Commit

Permalink
unique_ptr : 三方比較演算子の追加と自動導出に対応 #900
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Dec 13, 2022
1 parent 584eb3e commit 5767897
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
3 changes: 2 additions & 1 deletion reference/memory/unique_ptr.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ namespace std {
## 非メンバ関数
| 名前 | 説明 | 対応バージョン |
|--------------------------------------------------|-------------------------------------------|-------|
|------------------------------------------------|-------------------------------------------|-------|
| [`operator==`](unique_ptr/op_equal.md) | 等値比較 | C++11 |
| [`operator!=`](unique_ptr/op_not_equal.md) | 非等値比較 | C++11 |
| [`operator<=>`](unique_ptr/op_compare_3way.md) | 三方比較 | C++20 |
| [`operator<`](unique_ptr/op_less.md) | 左辺が右辺より小さいかを判定する | C++11 |
| [`operator<=`](unique_ptr/op_less_equal.md) | 左辺が右辺以下かを判定する | C++11 |
| [`operator>`](unique_ptr/op_greater.md) | 左辺が右辺より大きいかを判定する | C++11 |
Expand Down
98 changes: 98 additions & 0 deletions reference/memory/unique_ptr/op_compare_3way.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# operator<=>
* memory[meta header]
* std[meta namespace]
* function template[meta id-type]
* cpp20[meta cpp]

```cpp
namespace std {
template <class T1, class D1, class T2, class D2>
requires three_way_comparable_with<
typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
compare_three_way_result_t<
typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // (1) C++20

template <class T, class D>
requires three_way_comparable_with<
typename unique_ptr<T, D>::pointer,
nullptr_t>
compare_three_way_result_t<
typename unique_ptr<T, D>::pointer,
nullptr_t>
operator<=>(const unique_ptr<T, D>& x, nullptr_t); // (2) C++20
}
```
* nullptr_t[link /reference/cstddef/nullptr_t.md]
## 概要
`unique_ptr`オブジェクトの三方比較を行う。
## テンプレートパラメータ制約
- (1) : 型`unique_ptr<T1, D1>::pointer`と型`unique_ptr<T2, D2>::pointer`が三方比較可能であること
- (2) : 型`unique_ptr<T, D>::pointer`と型[`nullptr_t`](/reference/cstddef/nullptr_t.md)が三方比較可能であること
## 戻り値
- (1) :
```cpp
return compare_three_way()(x.get(), y.get());
```
* compare_three_way[link /reference/compare/compare_three_way.md]
* get()[link get.md]

- (2) :
```cpp
return compare_three_way()(x.get(), nullptr);
```
* compare_three_way[link /reference/compare/compare_three_way.md]
* get()[link get.md]


## 例
```cpp example
#include <iostream>
#include <memory>

int main()
{
std::unique_ptr<int> p1(new int(3));
if ((p1 <=> p1) == 0) {
std::cout << "equal" << std::endl;
}

std::unique_ptr<int> p2;
if ((p2 <=> nullptr) == 0) {
std::cout << "p2 is nullptr" << std::endl;
}

if ((nullptr <=> p2) == 0) {
std::cout << "p2 is nullptr" << std::endl;
}
}
```

### 出力
```
equal
p2 is nullptr
p2 is nullptr
```

## バージョン
### 言語
- C++20

### 処理系
- [Clang](/implementation.md#clang):
- [GCC](/implementation.md#gcc): 10
- [Visual C++](/implementation.md#visual_cpp): ??


## 参照
- [P1614R2 The Mothership has Landed](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1614r2.html)
- C++20での三方比較演算子の追加と、関連する演算子の自動導出
17 changes: 14 additions & 3 deletions reference/memory/unique_ptr/op_equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
```cpp
namespace std {
template <class T1, class D1, class T2, class D2>
bool operator==(const unique_ptr<T1, D1>& a, const unique_ptr<T2, D2>& b); // (1)
bool operator==(const unique_ptr<T1, D1>& a, const unique_ptr<T2, D2>& b); // (1) C++11

template <class T, class D>
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; // (2)
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; // (2) C++11

// (2)により以下のオーバーロードが使用可能 (C++20)
template <class T, class D>
bool operator==(nullptr_t, const unique_ptr<T, D>& x) noexcept; // (3)
bool operator==(nullptr_t, const unique_ptr<T, D>& x) noexcept; // (3) C++11
}
```
* nullptr_t[link /reference/cstddef/nullptr_t.md]
Expand All @@ -27,6 +28,11 @@ namespace std {
- (2), (3) : `!x`
## 備考
- この演算子により、以下の演算子が使用可能になる (C++20):
- `operator!=`
## 例
```cpp example
#include <iostream>
Expand Down Expand Up @@ -67,3 +73,8 @@ p2 is nullptr
- [ICC](/implementation.md#icc): ?
- [Visual C++](/implementation.md#visual_cpp): 2010, 2012, 2013
- 2012までは`nullptr`バージョンがない。


## 参照
- [P1614R2 The Mothership has Landed](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1614r2.html)
- C++20での三方比較演算子の追加と、関連する演算子の自動導出
12 changes: 9 additions & 3 deletions reference/memory/unique_ptr/op_not_equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

```cpp
namespace std {
// operator==により、以下の演算子が使用可能になる (C++20)
template <class T1, class D1, class T2, class D2>
bool operator!=(const unique_ptr<T1, D1>& a, const unique_ptr<T2, D2>& b); // (1)
bool operator!=(const unique_ptr<T1, D1>& a, const unique_ptr<T2, D2>& b); // (1) C++11

template <class T, class D>
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; // (2)
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; // (2) C++11

template <class T, class D>
bool operator!=(nullptr_t, const unique_ptr<T, D>& x) noexcept; // (3)
bool operator!=(nullptr_t, const unique_ptr<T, D>& x) noexcept; // (3) C++11
}
```
* nullptr_t[link /reference/cstddef/nullptr_t.md]
Expand Down Expand Up @@ -68,3 +69,8 @@ p3 is not nullptr
- [ICC](/implementation.md#icc): ?
- [Visual C++](/implementation.md#visual_cpp): 2010, 2012, 2013
- 2012までは`nullptr`バージョンがない。


## 参照
- [P1614R2 The Mothership has Landed](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1614r2.html)
- C++20での三方比較演算子の追加と、関連する演算子の自動導出

0 comments on commit 5767897

Please sign in to comment.