Standard library header <compare>
From cppreference.com
This header is part of the language support library.
Concepts | ||
specifies that operator <=> produces consistent result on given types (concept) | ||
Classes | ||
(C++20) | the result type of 3-way comparison that supports all 6 operators, is not substitutable, and allows incomparable values (class) | |
(C++20) | the result type of 3-way comparison that supports all 6 operators and is not substitutable (class) | |
(C++20) | the result type of 3-way comparison that supports all 6 operators and is substitutable (class) | |
(C++20) | the strongest comparison category to which all of the given types can be converted (class template) | |
(C++20) | obtains the result type of the three-way comparison operator <=> on given types (class template) | |
(C++20) | function object implementing x <=> y (class) | |
Customization point objects | ||
(C++20) | performs 3-way comparison and produces a result of type std::strong_ordering (customization point object) | |
(C++20) | performs 3-way comparison and produces a result of type std::weak_ordering (customization point object) | |
(C++20) | performs 3-way comparison and produces a result of type std::partial_ordering (customization point object) | |
performs 3-way comparison and produces a result of type std::strong_ordering , even if operator<=> is unavailable (customization point object) | ||
(C++20) | performs 3-way comparison and produces a result of type std::weak_ordering , even if operator<=> is unavailable (customization point object) | |
performs 3-way comparison and produces a result of type std::partial_ordering , even if operator<=> is unavailable (customization point object) | ||
Functions | ||
named comparison functions (function) |
[edit]Synopsis
namespace std {// comparison category typesclass partial_ordering;class weak_ordering;class strong_ordering; // named comparison functionsconstexprbool is_eq (partial_ordering cmp)noexcept{return cmp ==0;}constexprbool is_neq (partial_ordering cmp)noexcept{return cmp !=0;}constexprbool is_lt (partial_ordering cmp)noexcept{return cmp <0;}constexprbool is_lteq(partial_ordering cmp)noexcept{return cmp <=0;}constexprbool is_gt (partial_ordering cmp)noexcept{return cmp >0;}constexprbool is_gteq(partial_ordering cmp)noexcept{return cmp >=0;} // common comparison category typetemplate<class... Ts>struct common_comparison_category {using type =/* see description */;};template<class... Ts>using common_comparison_category_t =typename common_comparison_category<Ts...>::type; // concept three_way_comparabletemplate<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 comparisontemplate<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; // class compare_three_waystruct compare_three_way; // comparison algorithmsinlinenamespace/* unspecified */{inlineconstexpr/* unspecified */ strong_order =/* unspecified */;inlineconstexpr/* unspecified */ weak_order =/* unspecified */;inlineconstexpr/* unspecified */ partial_order =/* unspecified */;inlineconstexpr/* unspecified */ compare_strong_order_fallback =/* unspecified */;inlineconstexpr/* unspecified */ compare_weak_order_fallback =/* unspecified */;inlineconstexpr/* unspecified */ compare_partial_order_fallback =/* unspecified */;}}
[edit]Concept three_way_comparable
namespace std {template<class T, class Cat> concept __ComparesAs =// exposition only same_as<common_comparison_category_t<T, Cat>, Cat>; template<class T, class U> concept __PartiallyOrderedWith =// 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 = __WeaklyEqualityComparableWith<T, T>&& __PartiallyOrderedWith<T, T>&& requires(const remove_reference_t<T>& a, const remove_reference_t<T>& b){{ a <=> b }-> __ComparesAs<Cat>;};}
[edit]Concept 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>;};}
[edit]Class std::partial_ordering
namespace std {class partial_ordering {int value;// exposition onlybool is_ordered;// exposition only // exposition-only constructorsconstexprexplicit partial_ordering(eq v)noexcept: value(int(v)), is_ordered(true){}// exposition onlyconstexprexplicit partial_ordering(ord v)noexcept: value(int(v)), is_ordered(true){}// exposition onlyconstexprexplicit partial_ordering(ncmp v)noexcept: value(int(v)), is_ordered(false){}// exposition only public:// valid valuesstaticconst partial_ordering less;staticconst partial_ordering equivalent;staticconst partial_ordering greater;staticconst partial_ordering unordered; // comparisonsfriendconstexprbool operator==(partial_ordering v, /* unspecified */)noexcept;friendconstexprbool operator==(partial_ordering v, partial_ordering w)noexcept=default;friendconstexprbool operator<(partial_ordering v, /* unspecified */)noexcept;friendconstexprbool operator>(partial_ordering v, /* unspecified */)noexcept;friendconstexprbool operator<=(partial_ordering v, /* unspecified */)noexcept;friendconstexprbool operator>=(partial_ordering v, /* unspecified */)noexcept;friendconstexprbool operator<(/* unspecified */, partial_ordering v)noexcept;friendconstexprbool operator>(/* unspecified */, partial_ordering v)noexcept;friendconstexprbool operator<=(/* unspecified */, partial_ordering v)noexcept;friendconstexprbool operator>=(/* unspecified */, partial_ordering v)noexcept;friendconstexpr partial_ordering operator<=>(partial_ordering v, /* unspecified */)noexcept;friendconstexpr partial_ordering operator<=>(/* unspecified */, partial_ordering v)noexcept;}; // valid values' definitionsinlineconstexpr partial_ordering partial_ordering::less(ord::less);inlineconstexpr partial_ordering partial_ordering::equivalent(eq::equivalent);inlineconstexpr partial_ordering partial_ordering::greater(ord::greater);inlineconstexpr partial_ordering partial_ordering::unordered(ncmp::unordered);}
[edit]Class std::weak_ordering
namespace std {class weak_ordering {int value;// exposition only // exposition-only constructorsconstexprexplicit weak_ordering(eq v)noexcept: value(int(v)){}// exposition onlyconstexprexplicit weak_ordering(ord v)noexcept: value(int(v)){}// exposition only public:// valid valuesstaticconst weak_ordering less;staticconst weak_ordering equivalent;staticconst weak_ordering greater; // conversionsconstexpr operator partial_ordering()constnoexcept; // comparisonsfriendconstexprbool operator==(weak_ordering v, /* unspecified */)noexcept;friendconstexprbool operator==(weak_ordering v, weak_ordering w)noexcept=default;friendconstexprbool operator<(weak_ordering v, /* unspecified */)noexcept;friendconstexprbool operator>(weak_ordering v, /* unspecified */)noexcept;friendconstexprbool operator<=(weak_ordering v, /* unspecified */)noexcept;friendconstexprbool operator>=(weak_ordering v, /* unspecified */)noexcept;friendconstexprbool operator<(/* unspecified */, weak_ordering v)noexcept;friendconstexprbool operator>(/* unspecified */, weak_ordering v)noexcept;friendconstexprbool operator<=(/* unspecified */, weak_ordering v)noexcept;friendconstexprbool operator>=(/* unspecified */, weak_ordering v)noexcept;friendconstexpr weak_ordering operator<=>(weak_ordering v, /* unspecified */)noexcept;friendconstexpr weak_ordering operator<=>(/* unspecified */, weak_ordering v)noexcept;}; // valid values' definitionsinlineconstexpr weak_ordering weak_ordering::less(ord::less);inlineconstexpr weak_ordering weak_ordering::equivalent(eq::equivalent);inlineconstexpr weak_ordering weak_ordering::greater(ord::greater);}
[edit]Class std::strong_ordering
namespace std {class strong_ordering {int value;// exposition only // exposition-only constructorsconstexprexplicit strong_ordering(eq v)noexcept: value(int(v)){}// exposition onlyconstexprexplicit strong_ordering(ord v)noexcept: value(int(v)){}// exposition only public:// valid valuesstaticconst strong_ordering less;staticconst strong_ordering equal;staticconst strong_ordering equivalent;staticconst strong_ordering greater; // conversionsconstexpr operator partial_ordering()constnoexcept;constexpr operator weak_ordering()constnoexcept; // comparisonsfriendconstexprbool operator==(strong_ordering v, /* unspecified */)noexcept;friendconstexprbool operator==(strong_ordering v, strong_ordering w)noexcept=default;friendconstexprbool operator<(strong_ordering v, /* unspecified */)noexcept;friendconstexprbool operator>(strong_ordering v, /* unspecified */)noexcept;friendconstexprbool operator<=(strong_ordering v, /* unspecified */)noexcept;friendconstexprbool operator>=(strong_ordering v, /* unspecified */)noexcept;friendconstexprbool operator<(/* unspecified */, strong_ordering v)noexcept;friendconstexprbool operator>(/* unspecified */, strong_ordering v)noexcept;friendconstexprbool operator<=(/* unspecified */, strong_ordering v)noexcept;friendconstexprbool operator>=(/* unspecified */, strong_ordering v)noexcept;friendconstexpr strong_ordering operator<=>(strong_ordering v, /* unspecified */)noexcept;friendconstexpr strong_ordering operator<=>(/* unspecified */, strong_ordering v)noexcept;}; // valid values' definitionsinlineconstexpr strong_ordering strong_ordering::less(ord::less);inlineconstexpr strong_ordering strong_ordering::equal(eq::equal);inlineconstexpr strong_ordering strong_ordering::equivalent(eq::equivalent);inlineconstexpr strong_ordering strong_ordering::greater(ord::greater);}
[edit]Class std::compare_three_way
namespace std {struct compare_three_way {template<class T, class U> requires three_way_comparable_with<T, U>||/*BUILTIN-PTR-THREE-WAY*/(T, U)constexprauto operator()(T&& t, U&& u)const; using is_transparent =/* unspecified */;};}
[edit]See also
C++ documentation for three-way comparison operator (C++20) |