std::compare_three_way
From cppreference.com
| ヘッダ <compare> で定義 |
||
| ヘッダ <functional> で定義 |
||
| struct compare_three_way; |
(C++20以降) | |
比較を実行する関数オブジェクト。関数呼び出し演算子のパラメータ型と戻り値の型を推論します。
目次 |
[編集] ネストされた型
| ネストされた型 | 定義 |
is_transparent
|
unspecified |
[編集] メンバ関数
| operator() |
両方の引数に対して3方向比較の結果を取得します。 (public member function) |
std::compare_three_way::operator()
| template< class T, class U > constexpr auto operator()( T&& t, U&& u ) const; |
||
式 std::forward<T>(t) <=> std::forward<U>(u) を expr として
- 変換されたポインタ(型
P)を、実装定義のポインタに関する厳密な全順序で比較します。
- t が u より前に来る場合、std::strong_ordering::less を返します。
- u が t より前に来る場合、std::strong_ordering::greater を返します。
- それ以外の場合、std::strong_ordering::equal を返します。
TからPへの変換シーケンス、またはUからPへの変換シーケンスが 等価性を保持しない 場合、動作は未定義です。
- 変換されたポインタ(型
- それ以外の場合
- expr の結果を返します。
- もし std::three_way_comparable_with<T, U> がモデル化されていない場合、動作は未定義です。
このオーバーロードは、std::three_way_comparable_with<T, U> が満たされている場合にのみ、オーバーロード解決に参加します。
[編集] 例
このコードを実行
#include <compare> #include <iostream> struct Rational { int num; int den; // > 0 // Although the comparison X <=> Y will work, a direct call // to std::compare_three_way{}(X, Y) requires the operator== // be defined, to satisfy the std::three_way_comparable_with. constexpr bool operator==(Rational const&) const = default; }; constexpr std::weak_ordering operator<=>(Rational lhs, Rational rhs) { return lhs.num * rhs.den <=> rhs.num * lhs.den; } void print(std::weak_ordering value) { value < 0 ? std::cout << "less\n" : value > 0 ? std::cout << "greater\n" : std::cout << "equal\n"; } int main() { Rational a{6, 5}; Rational b{8, 7}; print(a <=> b); print(std::compare_three_way{}(a, b)); }
出力
greater greater
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3530 | C++20 | ポインタを比較する際の構文チェックが緩和されました。 | 意味的な要件のみが緩和されています。 |
[編集] 関連項目
| (C++20) |
x == y を実装する制約付き関数オブジェクト (クラス) |
| (C++20) |
x != y を実装する制約付き関数オブジェクト (クラス) |
| (C++20) |
x < y を実装する制約付き関数オブジェクト (クラス) |
| (C++20) |
x > y を実装する制約付き関数オブジェクト (クラス) |
| (C++20) |
x <= y を実装する制約付き関数オブジェクト (クラス) |
| (C++20) |
x >= y を実装する制約付き関数オブジェクト (クラス) |