名稱空間
變體
操作

std::totally_ordered, std::totally_ordered_with

來自 cppreference.com
< cpp‎ | 概念
定義於標頭檔案 <concepts>
template< class T >

concept totally_ordered =

    std::equality_comparable<T> && __PartiallyOrderedWith<T, T>;
(1) (C++20 起)
template< class T, class U >

concept totally_ordered_with =
    std::totally_ordered<T> &&
    std::totally_ordered<U> &&
    std::equality_comparable_with<T, U> &&
    std::totally_ordered<
        std::common_reference_t<
            const std::remove_reference_t<T>&,
            const std::remove_reference_t<U>&>> &&

    __PartiallyOrderedWith<T, U>;
(2) (C++20 起)
輔助概念
template< class T, class U >

concept __PartiallyOrderedWith =
    requires(const std::remove_reference_t<T>& t,
             const std::remove_reference_t<U>& u) {
        { t <  u } -> boolean-testable;
        { t >  u } -> boolean-testable;
        { t <= u } -> boolean-testable;
        { t >= u } -> boolean-testable;
        { u <  t } -> boolean-testable;
        { u >  t } -> boolean-testable;
        { u <= t } -> boolean-testable;
        { u >= t } -> boolean-testable;

    };
(3) (僅作說明*)
1) 概念 std::totally_ordered 指定型別上的比較運算子 ==,!=,<,>,<=,>= 的結果與該型別上的 嚴格全序 一致。
2) 概念 std::totally_ordered_with 指定 (可能混合的) TU 運算元上的比較運算子 ==,!=,<,>,<=,>= 的結果與嚴格全序一致。比較混合運算元會產生與比較轉換為其共同型別的運算元等效的結果。
3) 僅用於闡釋的概念 __PartiallyOrderedWith 指定型別為 T 的值和型別為 U 的值可以使用 <, >, <=>= 互相進行偏序比較 (以任一順序),並且比較結果是一致的。

目錄

[編輯] 語義要求

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

1) std::totally_ordered<T> 僅當給定型別為 const std::remove_reference_t<T> 的左值 abc 時,以下條件成立:
  • bool(a < b), bool(a > b)bool(a == b) 中恰好有一個為 true
  • 如果 bool(a < b)bool(b < c) 都為 true,則 bool(a < c)true
  • bool(a > b) == bool(b < a)
  • bool(a >= b) == !bool(a < b)
  • bool(a <= b) == !bool(b < a)
2) std::totally_ordered_with<T, U> 僅當給定以下條件時成立:

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 起)

以下條件為真

  • bool(t < u) == bool(CONVERT_TO<C>(t2) < CONVERT_TO<C>(u2))
  • bool(t > u) == bool(CONVERT_TO<C>(t2) > CONVERT_TO<C>(u2))
  • bool(t <= u) == bool(CONVERT_TO<C>(t2) <= CONVERT_TO<C>(u2))
  • bool(t >= u) == bool(CONVERT_TO<C>(t2) >= CONVERT_TO<C>(u2))
  • bool(u < t) == bool(CONVERT_TO<C>(u2) < CONVERT_TO<C>(t2))
  • bool(u > t) == bool(CONVERT_TO<C>(u2) > CONVERT_TO<C>(t2))
  • bool(u <= t) == bool(CONVERT_TO<C>(u2) <= CONVERT_TO<C>(t2))
  • bool(u >= t) == bool(CONVERT_TO<C>(u2) >= CONVERT_TO<C>(t2))
3) __PartiallyOrderedWith<T, U> 僅當給定以下條件時成立:

以下條件為真

  • t < u, t <= u, t > u, t >= u, u < t, u <= t, u > tu >= t 擁有相同的域;
  • bool(t < u) == bool(u > t);
  • bool(u < t) == bool(t > u);
  • bool(t <= u) == bool(u >= t);以及
  • bool(u <= t) == bool(t >= u).

[編輯] 相等性保持

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

[編輯] 隱式表示式變體

一個 requires 表示式 如果使用了對於某些常量左值運算元而言是非修改性的表示式,則也需要 隱式表示式變體

[編輯] 參考資料

  • C++23 標準 (ISO/IEC 14882:2024)
  • 18.5.5 概念 totally_ordered [concept.totallyordered]
  • C++20 標準 (ISO/IEC 14882:2020)
  • 18.5.4 概念 totally_ordered [concept.totallyordered]

[編輯] 另請參閱

指定運算子 <=> 在給定型別上產生一致的結果
(概念) [編輯]