Spec-Zone.ru › C++

Заголовок стандартной библиотеки <compare> (C++20)

Этот заголовок входит в библиотеку поддержки языка.

Концепции

three_way_comparablethree_way_comparable_with
(C++20)(C++20)
задаёт требование, чтобы оператор <=> выдавал согласованный результат для заданных типов
(концепция)

Классы

partial_ordering
(C++20)
тип результата трёхстороннего сравнения, поддерживающий все 6 операторов, не являющийся взаимозаменяемым и допускающий несравнимые значения
(класс)
weak_ordering
(C++20)
тип результата трёхстороннего сравнения, поддерживающий все 6 операторов и не являющийся взаимозаменяемым
(класс)
strong_ordering
(C++20)
тип результата трёхстороннего сравнения, поддерживающий все 6 операторов и являющийся взаимозаменяемым
(класс)
common_comparison_category
(C++20)
наиболее строгая категория сравнения, к которой можно преобразовать все заданные типы
(шаблон класса)
compare_three_way_result
(C++20)
получает тип результата оператора трёхстороннего сравнения <=> для заданных типов
(шаблон класса)
compare_three_way
(C++20)
функциональный объект с ограничениями, реализующий x <=> y
(класс)
type_order
(C++26)
выполняет трёхстороннее сравнение типов
(шаблон класса)

Объекты точек настройки

strong_order
(C++20)
выполняет трёхстороннее сравнение и возвращает результат типа std::strong_ordering
(объект точки настройки)
weak_order
(C++20)
выполняет трёхстороннее сравнение и возвращает результат типа std::weak_ordering
(объект точки настройки)
partial_order
(C++20)
выполняет трёхстороннее сравнение и возвращает результат типа std::partial_ordering
(объект точки настройки)
compare_strong_order_fallback
(C++20)
выполняет трёхстороннее сравнение и возвращает результат типа std::strong_ordering, даже если operator<=> недоступен
(объект точки настройки)
compare_weak_order_fallback
(C++20)
выполняет трёхстороннее сравнение и возвращает результат типа std::weak_ordering, даже если operator<=> недоступен
(объект точки настройки)
compare_partial_order_fallback
(C++20)
выполняет трёхстороннее сравнение и возвращает результат типа std::partial_ordering, даже если operator<=> недоступен
(объект точки настройки)

Функции

is_eqis_neqis_ltis_lteqis_gtis_gteq
(C++20)
именованные функции сравнения
(функция)

Обзор

// all freestanding
namespace std {
  // comparison category types
  class partial_ordering;
  class weak_ordering;
  class strong_ordering;

  // named comparison functions
  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; }

  // common comparison category type
  template<class... Ts> struct common_comparison_category
  {
    using type = /* see description */;
  };
  template<class... Ts>
  using common_comparison_category_t = common_comparison_category<Ts...>::type;

  // concept three_way_comparable
  template<class T, class Cat = partial_ordering>
  concept three_way_comparable = /* see description */;
  template<class T, class U, class Cat = partial_ordering>
  concept three_way_comparable_with = /* see description */;

  // result of three-way comparison
  template<class T, class U = T> struct compare_three_way_result;

  template<class T, class U = T>
  using compare_three_way_result_t = compare_three_way_result<T, U>::type;

  // class compare_three_way
  struct compare_three_way;

  // comparison algorithms
  inline namespace /* unspecified */ {
    inline constexpr /* unspecified */ strong_order = /* unspecified */;
    inline constexpr /* unspecified */ weak_order = /* unspecified */;
    inline constexpr /* unspecified */ partial_order = /* unspecified */;
    inline constexpr /* unspecified */ compare_strong_order_fallback = /* unspecified */;
    inline constexpr /* unspecified */ compare_weak_order_fallback = /* unspecified */;
    inline constexpr /* unspecified */ compare_partial_order_fallback = /* unspecified */;
  }

  // type ordering
  template<class T, class U> struct type_order;

  template<class T, class U>
  constexpr strong_ordering type_order_v = type_order<T, U>::value;
}

Концепция three_way_comparable

namespace std {
template<class T, class Cat>
concept /*compares-as*/ =            // exposition-only
  same_as<common_comparison_category_t<T, Cat>, Cat>;

template<class T, class U>
concept /*partially-ordered-with*/ = // exposition-only
  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 =
  /*weakly-equality-comparable-with*/<T, T> && /*partially-ordered-with*/<T, T> &&
  requires(const remove_reference_t<T>& a, const remove_reference_t<T>& b) {
    { a <=> b } -> /*compares-as*/<Cat>;
  };
}

Концепция three_way_comparable_with

namespace std {
template<class T, class U, class Cat = partial_ordering>
concept three_way_comparable_with =
  three_way_comparable<T, Cat> && three_way_comparable<U, Cat> &&
  /*comparison-common-type-with*/<T, U> &&
  three_way_comparable<
    common_reference_t<const remove_reference_t<T>&, const remove_reference_t<U>&>,
    Cat> &&
  /*weakly-equality-comparable-with*/<T, U> && /*partially-ordered-with*/<T, U> &&
  requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) {
    { t <=> u } -> /*compares-as*/<Cat>;
    { u <=> t } -> /*compares-as*/<Cat>;
  };
}

Класс std::partial_ordering

namespace std {
  class partial_ordering
  {
    int /*value*/;       // exposition-only
    bool /*is-ordered*/; // exposition-only

    // exposition-only constructors
    constexpr explicit partial_ordering(/*ord*/ v) noexcept
      : /*value*/(int(v))
      , /*is-ordered*/(true)
    {
    } // exposition-only
    constexpr explicit partial_ordering(/*ncmp*/ v) noexcept
      : /*value*/(int(v))
      , /*is-ordered*/(false)
    {
    } // exposition-only

  public:
    // valid values
    static const partial_ordering less;
    static const partial_ordering equivalent;
    static const partial_ordering greater;
    static const partial_ordering unordered;

    // comparisons
    friend constexpr bool operator==(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator==(partial_ordering v,
                                     partial_ordering w) noexcept = default;
    friend constexpr bool operator<(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<=(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>=(partial_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<(/* unspecified */, partial_ordering v) noexcept;
    friend constexpr bool operator>(/* unspecified */, partial_ordering v) noexcept;
    friend constexpr bool operator<=(/* unspecified */, partial_ordering v) noexcept;
    friend constexpr bool operator>=(/* unspecified */, partial_ordering v) noexcept;
    friend constexpr partial_ordering operator<=>(partial_ordering v,
                                                  /* unspecified */) noexcept;
    friend constexpr partial_ordering operator<=>(/* unspecified */,
                                                  partial_ordering v) noexcept;
  };

  // valid values' definitions
  inline constexpr partial_ordering partial_ordering::less(/*ord*/::/*less*/);
  inline constexpr partial_ordering partial_ordering::equivalent(/*ord*/::/*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*/; // exposition-only

    // exposition-only constructors
    constexpr explicit weak_ordering(/*ord*/ v) noexcept
      : /*value*/(int(v))
    {
    } // exposition-only

  public:
    // valid values
    static const weak_ordering less;
    static const weak_ordering equivalent;
    static const weak_ordering greater;

    // conversions
    constexpr operator partial_ordering() const noexcept;

    // comparisons
    friend constexpr bool operator==(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator==(weak_ordering v, weak_ordering w) noexcept = default;
    friend constexpr bool operator<(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<=(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>=(weak_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<(/* unspecified */, weak_ordering v) noexcept;
    friend constexpr bool operator>(/* unspecified */, weak_ordering v) noexcept;
    friend constexpr bool operator<=(/* unspecified */, weak_ordering v) noexcept;
    friend constexpr bool operator>=(/* unspecified */, weak_ordering v) noexcept;
    friend constexpr weak_ordering operator<=>(weak_ordering v,
                                               /* unspecified */) noexcept;
    friend constexpr weak_ordering operator<=>(/* unspecified */,
                                               weak_ordering v) noexcept;
  };

  // valid values' definitions
  inline constexpr weak_ordering weak_ordering::less(/*ord*/::/*less*/);
  inline constexpr weak_ordering weak_ordering::equivalent(/*ord*/::/*equivalent*/);
  inline constexpr weak_ordering weak_ordering::greater(/*ord*/::/*greater*/);
}

Класс std::strong_ordering

namespace std {
  class strong_ordering
  {
    int /*value*/; // exposition-only

    // exposition-only constructors
    constexpr explicit strong_ordering(/*ord*/ v) noexcept
      : /*value*/(int(v))
    {
    } // exposition-only

  public:
    // valid values
    static const strong_ordering less;
    static const strong_ordering equal;
    static const strong_ordering equivalent;
    static const strong_ordering greater;

    // conversions
    constexpr operator partial_ordering() const noexcept;
    constexpr operator weak_ordering() const noexcept;

    // comparisons
    friend constexpr bool operator==(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator==(strong_ordering v,
                                     strong_ordering w) noexcept = default;
    friend constexpr bool operator<(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<=(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator>=(strong_ordering v, /* unspecified */) noexcept;
    friend constexpr bool operator<(/* unspecified */, strong_ordering v) noexcept;
    friend constexpr bool operator>(/* unspecified */, strong_ordering v) noexcept;
    friend constexpr bool operator<=(/* unspecified */, strong_ordering v) noexcept;
    friend constexpr bool operator>=(/* unspecified */, strong_ordering v) noexcept;
    friend constexpr strong_ordering operator<=>(strong_ordering v,
                                                 /* unspecified */) noexcept;
    friend constexpr strong_ordering operator<=>(/* unspecified */,
                                                 strong_ordering v) noexcept;
  };

  // valid values' definitions
  inline constexpr strong_ordering strong_ordering::less(/*ord*/::/*less*/);
  inline constexpr strong_ordering strong_ordering::equal(/*ord*/::/*equal*/);
  inline constexpr strong_ordering strong_ordering::equivalent(/*ord*/::/*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 = /* unspecified */;
  };
}

Класс std::type_order

namespace std {
  template<class T, class U> struct type_order {
    static constexpr strong_ordering value = /*TYPE-ORDER*/(T, U);
    
    using value_type = strong_ordering;
    
    constexpr operator value_type() const noexcept { return value; }
    constexpr value_type operator()() const noexcept { return value; }
  };
}

См. также

оператор трёхстороннего сравнения выражение lhs <=> rhs (C++20)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/header/compare

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API