名稱空間
變體
操作

std::three_way_comparable, std::three_way_comparable_with

來自 cppreference.com
< cpp‎ | 工具
 
 
工具庫
語言支援
型別支援(基本型別、RTTI)
庫特性測試宏 (C++20)
程式工具
變參函式
協程支援 (C++20)
契約支援 (C++26)
三路比較
three_way_comparablethree_way_comparable_with
(C++20)(C++20)
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

通用工具
關係運算符 (C++20 中棄用)
 
定義於標頭檔案 <compare>
template< class T, class Cat = std::partial_ordering >

concept three_way_comparable =
    __WeaklyEqualityComparableWith<T, T> &&
    __PartiallyOrderedWith<T, T> &&
    requires(const std::remove_reference_t<T>& a,
             const std::remove_reference_t<T>& b) {
        { a <=> b } -> __ComparesAs<Cat>;

    };
(1) (C++20 起)
template< class T, class U, class Cat = std::partial_ordering >

concept three_way_comparable_with =
    std::three_way_comparable<T, Cat> &&
    std::three_way_comparable<U, Cat> &&
    __ComparisonCommonTypeWith<T, U> &&
    std::three_way_comparable<
        std::common_reference_t<
            const std::remove_reference_t<T>&,
            const std::remove_reference_t<U>&>, Cat> &&
    __WeaklyEqualityComparableWith<T, U> &&
    __PartiallyOrderedWith<T, U> &&
    requires(const std::remove_reference_t<T>& t,
             const std::remove_reference_t<U>& u) {
        { t <=> u } -> __ComparesAs<Cat>;
        { u <=> t } -> __ComparesAs<Cat>;

    };
(2) (C++20 起)
template< class T, class Cat >

concept __ComparesAs =

    std::same_as<std::common_comparison_category_t<T, Cat>, Cat>;
(3) (僅作說明*)
1) 概念 std::three_way_comparable 指定對 T 的三路比較運算子 <=> 產生與 Cat 隱含的比較類別一致的結果。
2) 概念 std::three_way_comparable_with 指定對(可能混合的)TU 運算元的三路比較運算子 <=> 產生與 Cat 隱含的比較類別一致的結果。比較混合運算元產生的結果等同於比較轉換為其共同型別的運算元。

__WeaklyEqualityComparableWith__PartiallyOrderedWith__ComparisonCommonTypeWith 是僅用於闡釋的概念。請參見 equality_comparabletotally_ordered 的描述。

目錄

[編輯] 語義要求

這些概念僅當它們被滿足並且它們所包含的所有概念都被建模時才被建模。

1) 僅當給定型別為 const std::remove_reference_t<T> 的左值 ab 時,TCat 才符合 std::three_way_comparable<T, Cat> 模型,並且以下條件成立:
  • (a <=> b == 0) == bool(a == b),
  • (a <=> b != 0) == bool(a != b),
  • ((a <=> b) <=> 0)(0 <=> (b <=> a)) 相等,
  • bool(a > b) == bool(b < a),
  • bool(a >= b) == !bool(a < b),
  • bool(a <= b) == !bool(b < a),
  • (a <=> b < 0) == bool(a < b),
  • (a <=> b > 0) == bool(a > b),
  • (a <=> b <= 0) == bool(a <= b),以及
  • (a <=> b >= 0) == bool(a >= b),以及
  • 如果 Cat 可轉換為 std::strong_ordering,則 T 符合 totally_ordered 模型。
2) 僅當給定以下條件時,TUCat 才符合 std::three_way_comparable_with<T, U, Cat> 模型:

Cstd::common_reference_t<const std::remove_reference_t<T>&, const std::remove_reference_t<U>&>,且給定表示式 E 和型別 C,則 CONVERT_TO<C>(E)

(直至 C++23)
  • static_cast<const C&>(std::as_const(E))(如果這是有效表示式),
  • static_cast<const C&>(std::move(E))(否則)。
(C++23 起)

以下條件為真

  • t <=> uu <=> t 具有相同的域,
  • ((t <=> u) <=> 0)(0 <=> (u <=> t)) 相等,
  • (t <=> u == 0) == bool(t == u),
  • (t <=> u != 0) == bool(t != u),
  • Cat(t <=> u) == Cat(CONVERT_TO<C>(t2) <=> CONVERT_TO<C>(u2)),
  • (t <=> u < 0) == bool(t < u),
  • (t <=> u > 0) == bool(t > u),
  • (t <=> u <= 0) == bool(t <= u),
  • (t <=> u >= 0) == bool(t >= u),以及
  • 如果 Cat 可轉換為 std::strong_ordering,則 TU 符合 std::totally_ordered_with<T, U> 模型。

[編輯] 相等性保持

標準庫概念的 requires 表示式中宣告的表示式必須是相等性保持的(除非另有說明)。

[編輯] 隱式表示式變體

使用對於某個常量左值運算元是非修改表示式的 requires 表示式 也需要隱式表示式變體

[編輯] 另請參見

指定運算子 == 是等價關係
(概念) [編輯]
指定型別上的比較運算子產生全序關係
(概念) [編輯]