Namespaces
Variants

std:: strong_ordering

From cppreference.net
Utilities library
ヘッダーで定義 <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 は、その型のconst staticデータメンバとして実装された4つの有効な値を持ちます:

名前 定義
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への暗黙変換
(公開メンバ関数)

std::strong_ordering:: operator partial_ordering

constexpr operator partial_ordering ( ) const noexcept ;

戻り値

std::partial_ordering::less v less の場合、 std::partial_ordering::greater v greater の場合、 std::partial_ordering::equivalent v equal または equivalent の場合に返されます。

operator weak_ordering
std::weak_orderingへの暗黙変換 std::weak_ordering
(公開メンバ関数)

std::strong_ordering:: operator weak_ordering

constexpr operator weak_ordering ( ) const noexcept ;

戻り値

std::weak_ordering::less もし v less の場合、 std::weak_ordering::greater もし v greater の場合、 std::weak_ordering::equivalent もし v equal または equivalent の場合。

比較

比較演算子はこの型の値とリテラル 0 の間で定義されています。これにより、 a <=> b == 0 a <=> b < 0 といった式がサポートされ、三方比較演算子の結果をブーリアンの関係に変換するために使用できます。詳細は std::is_eq std::is_lt などを参照してください。

これらの関数は通常の unqualified lookup または qualified lookup では可視化されず、 std::strong_ordering が引数の関連クラスである場合にのみ argument-dependent lookup によって発見されます。

strong_ordering を整数リテラル 0 以外のものと比較しようとするプログラムの動作は未定義です。

operator== operator< operator> operator<= operator>= operator<=>
ゼロまたは strong_ordering と比較する
(関数)

operator==

friend constexpr bool
operator == ( strong_ordering v, /*unspecified*/ u ) noexcept ;
(1)
friend constexpr bool
operator == ( strong_ordering v, strong_ordering w ) noexcept = default ;
(2)

パラメータ

v, w - std::strong_ordering 値をチェックする
u - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) true v equivalent または equal の場合、 false v less または greater の場合
2) true 両方のパラメータが同じ値を保持する場合、 false それ以外の場合。 equal equivalent と同じであることに注意。

operator<

friend constexpr bool operator < ( strong_ordering v, /*unspecified*/ u ) noexcept ;
(1)
friend constexpr bool operator < ( /*unspecified*/ u, strong_ordering v ) noexcept ;
(2)

パラメータ

v - チェックする std::strong_ordering
u - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) true v less の場合は true v greater equivalent または equal の場合は false
2) true v greater の場合は true v less equivalent または equal の場合は false

operator<=

friend constexpr bool operator <= ( strong_ordering v, /*unspecified*/ u ) noexcept ;
(1)
friend constexpr bool operator <= ( /*unspecified*/ u, strong_ordering v ) noexcept ;
(2)

パラメータ

v - チェックする std::strong_ordering
u - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) true v less equivalent または equal の場合、および false v greater の場合
2) true v greater equivalent または equal の場合、および false v less の場合

operator>

friend constexpr bool operator > ( strong_ordering v, /*unspecified*/ u ) noexcept ;
(1)
friend constexpr bool operator > ( /*unspecified*/ u, strong_ordering v ) noexcept ;
(2)

パラメータ

v - チェックする std::strong_ordering
u - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) true もし v greater ならば、そして false もし v less equivalent または equal ならば
2) true もし v less ならば、そして false もし v greater equivalent または equal ならば

operator>=

friend constexpr bool operator >= ( strong_ordering v, /*unspecified*/ u ) noexcept ;
(1)
friend constexpr bool operator >= ( /*unspecified*/ u, strong_ordering v ) noexcept ;
(2)

パラメータ

v - チェックする std::strong_ordering
u - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) true v greater equivalent または equal の場合、および false v less の場合
2) true v less equivalent または equal の場合、および false v greater の場合

operator<=>

friend constexpr strong_ordering
operator <=> ( strong_ordering v, /*unspecified*/ u ) noexcept ;
(1)
friend constexpr strong_ordering
operator <=> ( /*unspecified*/ u, strong_ordering v ) noexcept ;
(2)

パラメータ

v - チェックする std::strong_ordering
u - リテラルゼロ引数を受け入れる任意の型の未使用パラメータ

戻り値

1) v
2) greater v less の場合)、 less v greater の場合)、それ以外の場合は 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 ? " >  " : " == " ) // 0と比較
              << q << '\n';
}
void print_two_way_comparison(const auto& p, const auto& q)
{
    std::cout << p
              << (p < q ? " <  " : p > q ? " >  " : " == ") // pと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つの演算子をサポートし、置換可能ではない三方比較の結果型
(class)
すべての6つの演算子をサポートし、置換可能ではなく、比較不能な値を許容する三方比較の結果型
(class)