Namespaces
Variants

std::strong_ordering

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート(基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
Swap と 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通の語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
ヘッダ <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)