Namespaces
Variants

Standard library header <compare> (C++20)

From cppreference.net
Standard library headers

此头文件属于 语言支持 库的一部分。

目录

概念

指定运算符 <=> 在给定类型上产生一致结果
(概念)

支持全部6个运算符、不可替换且允许不可比较值的三路比较结果类型
(类)
支持全部6个运算符且不可替换的三路比较结果类型
(类)
支持全部6个运算符且可替换的三路比较结果类型
(类)
所有给定类型均可转换到的最强比较类别
(类模板)
获取三路比较运算符 <=> 在给定类型上的结果类型
(类模板)
实现 x <=> y 的约束函数对象
(类)

定制点对象

执行三路比较并生成 std::strong_ordering 类型的结果
(定制点对象)
(C++20)
执行三路比较并生成 std::weak_ordering 类型的结果
(定制点对象)
执行三路比较并生成 std::partial_ordering 类型的结果
(定制点对象)
执行三路比较并生成 std::strong_ordering 类型的结果,即使 operator <=> 不可用
(定制点对象)
执行三路比较并生成 std::weak_ordering 类型的结果,即使 operator <=> 不可用
(定制点对象)
执行三路比较并生成 std::partial_ordering 类型的结果,即使 operator <=> 不可用
(定制点对象)

函数

<a href="../utility

概要

namespace std {
  // 比较类别类型
  class partial_ordering;
  class weak_ordering;
  class strong_ordering;
  // 命名比较函数
  constexpr bool is_eq  (partial_ordering cmp) noexcept { return cmp == 0; }
  constexpr bool is_neq (partial_ordering cmp) noexcept { return cmp != 0; }
  constexpr bool is_lt  (partial_ordering cmp) noexcept { return cmp < 0; }
  constexpr bool is_lteq(partial_ordering cmp) noexcept { return cmp <= 0; }
  constexpr bool is_gt  (partial_ordering cmp) noexcept { return cmp > 0; }
  constexpr bool is_gteq(partial_ordering cmp) noexcept { return cmp >= 0; }
  // 通用比较类别类型
  template<class... Ts>
  struct common_comparison_category {
    using type = /* 详见描述 */;
  };
  template<class... Ts>
    using common_comparison_category_t = typename common_comparison_category<Ts...>::type;
  // 概念 three_way_comparable
  template<class T, class Cat = partial_ordering>
    concept three_way_comparable = /* 详见描述 */;
  template<class T, class U, class Cat = partial_ordering>
    concept three_way_comparable_with = /* 详见描述 */;
  // 三路比较结果
  template<class T, class U = T> struct compare_three_way_result;
  template<class T, class U = T>
    using compare_three_way_result_t = typename compare_three_way_result<T, U>::type;
  // 类 compare_three_way
  struct compare_three_way;
  // 比较算法
  inline namespace /* 未指定 */ {
    inline constexpr /* 未指定 */ strong_order = /* 未指定 */;
    inline constexpr /* 未指定 */ weak_order = /* 未指定 */;
    inline constexpr /* 未指定 */ partial_order = /* 未指定 */;
    inline constexpr /* 未指定 */ compare_strong_order_fallback = /* 未指定 */;
    inline constexpr /* 未指定 */ compare_weak_order_fallback = /* 未指定 */;
    inline constexpr /* 未指定 */ compare_partial_order_fallback = /* 未指定 */;
  }
}

概念 three_way_comparable

namespace std {
  template<class T, class Cat>
    concept __ComparesAs =                // 仅用于说明
      same_as<common_comparison_category_t<T, Cat>, Cat>;
  template<class T, class U>
    concept __PartiallyOrderedWith =      // 仅用于说明
      requires(const remove_reference_t<T>& t, const 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;
      };
  template<class T, class Cat = partial_ordering>
    concept three_way_comparable =
      __WeaklyEqualityComparableWith<T, T> &&
      __PartiallyOrderedWith<T, T> &&
      requires(const remove_reference_t<T>& a, const remove_reference_t<T>& b) {
        { a <=> b } -> __ComparesAs<Cat>;
      };
}

概念 three_way_comparable_with

namespace std {
  template<class T, class U, class Cat = partial_ordering>
    concept three_way_comparable_with =
      __WeaklyEqualityComparableWith<T, U> &&
      __PartiallyOrderedWith<T, U> &&
      three_way_comparable<T, Cat> &&
      three_way_comparable<U, Cat> &&
      common_reference_with<const remove_reference_t<T>&, const remove_reference_t<U>&> &&
      three_way_comparable<
        common_reference_t<
          const remove_reference_t<T>&, const remove_reference_t<U>&>, Cat> &&
      requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) {
        { t <=> u } -> __ComparesAs<Cat>;
        { u <=> t } -> __ComparesAs<Cat>;
      };
}

std:: partial_ordering

namespace std {
  class partial_ordering {
    int value;          // 仅用于说明
    bool is_ordered;    // 仅用于说明
    // 仅用于说明的构造函数
    constexpr explicit
      partial_ordering(eq v) noexcept :
        value(int(v)), is_ordered(true) {}      // 仅用于说明
    constexpr explicit
      partial_ordering(ord v) noexcept :
        value(int(v)), is_ordered(true) {}     // 仅用于说明
    constexpr explicit
      partial_ordering(ncmp v) noexcept :
        value(int(v)), is_ordered(false) {}   // 仅用于说明
  public:
    // 有效值
    static const partial_ordering less;
    static const partial_ordering equivalent;
    static const partial_ordering greater;
    static const partial_ordering unordered;
    // 比较操作
    friend constexpr bool operator==(partial_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool
      operator==(partial_ordering v, partial_ordering w) noexcept = default;
    friend constexpr bool operator< (partial_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator> (partial_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator<=(partial_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator>=(partial_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator< (/* 未指定 */, partial_ordering v) noexcept;
    friend constexpr bool operator> (/* 未指定 */, partial_ordering v) noexcept;
    friend constexpr bool operator<=(/* 未指定 */, partial_ordering v) noexcept;
    friend constexpr bool operator>=(/* 未指定 */, partial_ordering v) noexcept;
    friend constexpr partial_ordering
      operator<=>(partial_ordering v, /* 未指定 */) noexcept;
    friend constexpr partial_ordering
      operator<=>(/* 未指定 */, partial_ordering v) noexcept;
  };
  // 有效值的定义
  inline constexpr partial_ordering partial_ordering::less(ord::less);
  inline constexpr partial_ordering partial_ordering::equivalent(eq::equivalent);
  inline constexpr partial_ordering partial_ordering::greater(ord::greater);
  inline constexpr partial_ordering partial_ordering::unordered(ncmp::unordered);
}

std:: weak_ordering

namespace std {
  class weak_ordering {
    int value;  // 仅用于说明
    // 仅用于说明的构造函数
    constexpr explicit weak_ordering(eq v) noexcept : value(int(v)) {}  // 仅用于说明
    constexpr explicit weak_ordering(ord v) noexcept : value(int(v)) {} // 仅用于说明
  public:
    // 有效值
    static const weak_ordering less;
    static const weak_ordering equivalent;
    static const weak_ordering greater;
    // 转换
    constexpr operator partial_ordering() const noexcept;
    // 比较操作
    friend constexpr bool operator==(weak_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator==(weak_ordering v, weak_ordering w) noexcept = default;
    friend constexpr bool operator< (weak_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator> (weak_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator<=(weak_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator>=(weak_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator< (/* 未指定 */, weak_ordering v) noexcept;
    friend constexpr bool operator> (/* 未指定 */, weak_ordering v) noexcept;
    friend constexpr bool operator<=(/* 未指定 */, weak_ordering v) noexcept;
    friend constexpr bool operator>=(/* 未指定 */, weak_ordering v) noexcept;
    friend constexpr weak_ordering
      operator<=>(weak_ordering v, /* 未指定 */) noexcept;
    friend constexpr weak_ordering
      operator<=>(/* 未指定 */, weak_ordering v) noexcept;
  };
  // 有效值的定义
  inline constexpr weak_ordering weak_ordering::less(ord::less);
  inline constexpr weak_ordering weak_ordering::equivalent(eq::equivalent);
  inline constexpr weak_ordering weak_ordering::greater(ord::greater);
}

std:: strong_ordering

namespace std {
  class strong_ordering {
    int value;  // 仅用于说明
    // 仅用于说明的构造函数
    constexpr explicit strong_ordering(eq v) noexcept :
      value(int(v)) {}    // 仅用于说明
    constexpr explicit strong_ordering(ord v) noexcept :
      value(int(v)) {}   // 仅用于说明
  public:
    // 有效值
    static const strong_ordering less;
    static const strong_ordering equal;
    static const strong_ordering equivalent;
    static const strong_ordering greater;
    // 转换
    constexpr operator partial_ordering() const noexcept;
    constexpr operator weak_ordering() const noexcept;
    // 比较操作
    friend constexpr bool operator==(strong_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool
      operator==(strong_ordering v, strong_ordering w) noexcept = default;
    friend constexpr bool operator< (strong_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator> (strong_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator<=(strong_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator>=(strong_ordering v, /* 未指定 */) noexcept;
    friend constexpr bool operator< (/* 未指定 */, strong_ordering v) noexcept;
    friend constexpr bool operator> (/* 未指定 */, strong_ordering v) noexcept;
    friend constexpr bool operator<=(/* 未指定 */, strong_ordering v) noexcept;
    friend constexpr bool operator>=(/* 未指定 */, strong_ordering v) noexcept;
    friend constexpr strong_ordering
      operator<=>(strong_ordering v, /* 未指定 */) noexcept;
    friend constexpr strong_ordering
    operator<=>(/* 未指定 */, strong_ordering v) noexcept;
  };
  // 有效值的定义
  inline constexpr strong_ordering strong_ordering::less(ord::less);
  inline constexpr strong_ordering strong_ordering::equal(eq::equal);
  inline constexpr strong_ordering strong_ordering::equivalent(eq::equivalent);
  inline constexpr strong_ordering strong_ordering::greater(ord::greater);
}

std:: compare_three_way

namespace std {
  struct compare_three_way {
    template<class T, class U>
    constexpr auto operator()(T&& t, U&& u) const;
    using is_transparent = /* 未指定 */;
  };
}

参见

三路比较运算符 表达式 lhs <=> rhs (C++20)