std::strong_ordering
| ヘッダ <compare> で定義 |
||
| class strong_ordering; |
(C++20以降) | |
クラス型std::strong_orderingは、三項比較の結果型であり、次の特徴を持ちます。
- 6つの関係演算子 (
==,!=,<,<=,>,>=) をすべて許容します。
- 代入可能性を保証します。もしaがbと等価であるならば、f(a)もf(b)と等価になります。ここで、fは、引数の公開constメンバを通じてアクセス可能な比較に関係する状態のみを読み取る関数を表します。言い換えれば、等価な値は区別できません。
- 比較不可能な値を許容しません。つまり、a < b, a == b, または a > b のいずれか1つだけがtrue にならなければなりません。
目次 |
[編集] 定数
型std::strong_orderingには4つの有効な値があり、その型の`const static`データメンバとして実装されています。
| 名前 | 定義 |
| inline constexpr std::strong_ordering less [static] |
より小さい(順序が前)関係を示す有効な値 (公開静的メンバ定数) |
| inline constexpr std::strong_ordering equivalent [static] |
等価(順序が前でも後でもない)関係を示す有効な値、equalと同じ(公開静的メンバ定数) |
| inline constexpr std::strong_ordering equal [static] |
等価(順序が前でも後でもない)関係を示す有効な値、equivalentと同じ(公開静的メンバ定数) |
| inline constexpr std::strong_ordering greater [static] |
より大きい(順序が後)関係を示す有効な値 (公開静的メンバ定数) |
[編集] 変換
std::strong_orderingは3つの比較カテゴリの中で最も強いものであり、他のカテゴリから暗黙的に変換されることはありませんが、他の2つには暗黙的に変換されます。
| operator partial_ordering |
std::partial_orderingへの暗黙変換 (public member function) |
std::strong_ordering::operator partial_ordering
| constexpr operator partial_ordering() const noexcept; |
||
戻り値
vがlessの場合はstd::partial_ordering::less、greaterの場合はstd::partial_ordering::greater、equalまたはequivalentの場合はstd::partial_ordering::equivalentを返します。
| operator weak_ordering |
std::weak_orderingへの暗黙変換 (public member function) |
std::strong_ordering::operator weak_ordering
| constexpr operator weak_ordering() const noexcept; |
||
戻り値
vがlessの場合はstd::weak_ordering::less、greaterの場合はstd::weak_ordering::greater、equalまたはequivalentの場合はstd::weak_ordering::equivalentを返します。
[編集] 比較
この型の値とリテラルの0との間で比較演算子が定義されています。これにより、三項比較演算子の結果をブール関係に変換するために使用できる式 a <=> b == 0 または a <=> b < 0 をサポートします。詳細はstd::is_eq、std::is_ltなどを参照してください。
これらの関数は、通常の非修飾または修飾ルックアップでは見えず、std::strong_orderingが引数に関連付けられたクラスである場合にのみ引数依存ルックアップによって見つけることができます。
strong_orderingをリテラル0以外のものと比較しようとするプログラムの動作は未定義です。
| operator==operator<operator>operator<=operator>=operator<=> |
ゼロまたはstrong_orderingとの比較(関数) |
operator==
| friend constexpr bool operator==( strong_ordering v, /*未指定*/ u ) noexcept; |
(1) | |
| friend constexpr bool operator==( strong_ordering v, strong_ordering w ) noexcept = default; |
(2) | |
パラメータ
| v, w | - | チェックするstd::strong_ordering値 |
| u | - | リテラルゼロ引数を受け入れる任意の型の未使用パラメータ |
戻り値
vがequivalentまたはequalの場合はtrue、lessまたはgreaterの場合はfalse。equalはequivalentと同じであることに注意してください。operator<
| friend constexpr bool operator<( strong_ordering v, /*未指定*/ u ) noexcept; |
(1) | |
| friend constexpr bool operator<( /*未指定*/ u, strong_ordering v ) noexcept; |
(2) | |
パラメータ
| v | - | チェックするstd::strong_ordering値 |
| u | - | リテラルゼロ引数を受け入れる任意の型の未使用パラメータ |
戻り値
vがlessの場合はtrue、greater、equivalent、またはequalの場合はfalse。vがgreaterの場合はtrue、less、equivalent、またはequalの場合はfalse。operator<=
| friend constexpr bool operator<=( strong_ordering v, /*未指定*/ u ) noexcept; |
(1) | |
| friend constexpr bool operator<=( /*未指定*/ u, strong_ordering v ) noexcept; |
(2) | |
パラメータ
| v | - | チェックするstd::strong_ordering値 |
| u | - | リテラルゼロ引数を受け入れる任意の型の未使用パラメータ |
戻り値
vがless、equivalent、またはequalの場合はtrue、greaterの場合はfalse。vがgreater、equivalent、またはequalの場合はtrue、lessの場合はfalse。operator>
| friend constexpr bool operator>( strong_ordering v, /*未指定*/ u ) noexcept; |
(1) | |
| friend constexpr bool operator>( /*未指定*/ u, strong_ordering v ) noexcept; |
(2) | |
パラメータ
| v | - | チェックするstd::strong_ordering値 |
| u | - | リテラルゼロ引数を受け入れる任意の型の未使用パラメータ |
戻り値
vがgreaterの場合はtrue、less、equivalent、またはequalの場合はfalse。vがlessの場合はtrue、greater、equivalent、またはequalの場合はfalse。operator>=
| friend constexpr bool operator>=( strong_ordering v, /*未指定*/ u ) noexcept; |
(1) | |
| friend constexpr bool operator>=( /*未指定*/ u, strong_ordering v ) noexcept; |
(2) | |
パラメータ
| v | - | チェックするstd::strong_ordering値 |
| u | - | リテラルゼロ引数を受け入れる任意の型の未使用パラメータ |
戻り値
vがgreater、equivalent、またはequalの場合はtrue、lessの場合はfalse。vがless、equivalent、またはequalの場合はtrue、greaterの場合はfalse。operator<=>
| friend constexpr strong_ordering operator<=>( strong_ordering v, /*未指定*/ u ) noexcept; |
(1) | |
| friend constexpr strong_ordering operator<=>( /*未指定*/ u, strong_ordering v ) noexcept; |
(2) | |
パラメータ
| v | - | チェックするstd::strong_ordering値 |
| u | - | リテラルゼロ引数を受け入れる任意の型の未使用パラメータ |
戻り値
vがlessの場合はgreater、greaterの場合はless、それ以外の場合はv。[編集] 例
#include <compare> #include <iostream> struct Point { int x{}, y{}; friend constexpr std::strong_ordering operator<=>(Point lhs, Point rhs) { if (lhs.x < rhs.x or (lhs.x == rhs.x and lhs.y < rhs.y)) return std::strong_ordering::less; if (lhs.x > rhs.x or (lhs.x == rhs.x and lhs.y > rhs.y)) return std::strong_ordering::greater; return std::strong_ordering::equivalent; } friend std::ostream& operator<<(std::ostream& os, Point s) { return os << '(' << s.x << ',' << s.y << ')'; } }; void print_three_way_comparison(const auto& p, const auto& q) { const auto cmp{p <=> q}; std::cout << p << (cmp < 0 ? " < " : cmp > 0 ? " > " : " == " ) // compares with 0 << q << '\n'; } void print_two_way_comparison(const auto& p, const auto& q) { std::cout << p << (p < q ? " < " : p > q ? " > " : " == ") // compares p and q << q << '\n'; } int main() { const Point p1{0, 1}, p2{0, 1}, p3{0, 2}; print_three_way_comparison(p1, p2); print_two_way_comparison(p1, p2); print_three_way_comparison(p2, p3); print_two_way_comparison(p2, p3); print_three_way_comparison(p3, p2); print_two_way_comparison(p3, p2); }
出力
(0,1) == (0,1) (0,1) == (0,1) (0,1) < (0,2) (0,1) < (0,2) (0,2) > (0,1) (0,2) > (0,1)
[編集] 関連項目
| (C++20) |
6つすべての演算子をサポートし、置換可能ではない三方比較の結果型 (クラス) |
| (C++20) |
6つすべての演算子をサポートし、置換可能ではなく、比較不可能な値を許容する三方比較の結果型 (クラス) |