名前空間
変種
操作

std::strong_ordering

From cppreference.com
< cpp‎ | utility
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
strong_ordering
(C++20)
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
ヘッダ <compare> で定義
class strong_ordering;
(C++20以降)

クラス型std::strong_orderingは、三項比較の結果型であり、次の特徴を持ちます。

  • 6つの関係演算子 (==, !=, <, <=, >, >=) をすべて許容します。
  • 代入可能性を保証します。もしabと等価であるならば、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;

戻り値

vlessの場合はstd::partial_ordering::lessgreaterの場合はstd::partial_ordering::greaterequalまたは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;

戻り値

vlessの場合はstd::weak_ordering::lessgreaterの場合はstd::weak_ordering::greaterequalまたはequivalentの場合はstd::weak_ordering::equivalentを返します。

[編集] 比較

この型の値とリテラルの0との間で比較演算子が定義されています。これにより、三項比較演算子の結果をブール関係に変換するために使用できる式 a <=> b == 0 または a <=> b < 0 をサポートします。詳細はstd::is_eqstd::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 - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) vequivalentまたはequalの場合はtruelessまたはgreaterの場合はfalse
2) 両方のパラメータが同じ値を持つ場合はtrue、それ以外の場合はfalseequalequivalentと同じであることに注意してください。

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 - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) vlessの場合はtruegreaterequivalent、またはequalの場合はfalse
2) vgreaterの場合はtruelessequivalent、または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 - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) vlessequivalent、またはequalの場合はtruegreaterの場合はfalse
2) vgreaterequivalent、またはequalの場合はtruelessの場合は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 - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) vgreaterの場合はtruelessequivalent、またはequalの場合はfalse
2) vlessの場合はtruegreaterequivalent、または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 - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) vgreaterequivalent、またはequalの場合はtruelessの場合はfalse
2) vlessequivalent、またはequalの場合はtruegreaterの場合は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 - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) v
2) vlessの場合はgreatergreaterの場合は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)

[編集] 関連項目

6つすべての演算子をサポートし、置換可能ではない三方比較の結果型
(クラス) [編集]
6つすべての演算子をサポートし、置換可能ではなく、比較不可能な値を許容する三方比較の結果型
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)