-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unique_ptr : 三方比較演算子の追加と自動導出に対応 #900
- Loading branch information
1 parent
584eb3e
commit 5767897
Showing
4 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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での三方比較演算子の追加と、関連する演算子の自動導出 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters