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

From cppreference.com

This header is part of the

language support

library.

Concepts

three_way_comparablethree_way_comparable_with

(C++20)(C++20)

specifies that operator <=> produces consistent result on given types
(concept)

[edit]

Classes

partial_ordering

(C++20)

the result type of 3-way comparison that supports all 6 operators, is not substitutable, and allows incomparable values
(class)

[edit]

weak_ordering

(C++20)

the result type of 3-way comparison that supports all 6 operators and is not substitutable
(class)

[edit]

strong_ordering

(C++20)

the result type of 3-way comparison that supports all 6 operators and is substitutable
(class)

[edit]

common_comparison_category

(C++20)

the strongest comparison category to which all of the given types can be converted
(class template)

[edit]

compare_three_way_result

(C++20)

obtains the result type of the three-way comparison operator <=> on given types
(class template)

[edit]

compare_three_way

(C++20)

constrained function object implementing x<=>y
(class)

[edit]

type_order

(C++26)

performs three-way comparison on types
(class template)

[edit]

Customization point objects

strong_order

(C++20)

performs 3-way comparison and produces a result of type std::strong_ordering
(customization point object)

[edit]

weak_order

(C++20)

performs 3-way comparison and produces a result of type std::weak_ordering
(customization point object)

[edit]

partial_order

(C++20)

performs 3-way comparison and produces a result of type std::partial_ordering
(customization point object)

[edit]

compare_strong_order_fallback

(C++20)

performs 3-way comparison and produces a result of type std::strong_ordering, even if operator<=> is unavailable
(customization point object)

[edit]

compare_weak_order_fallback

(C++20)

performs 3-way comparison and produces a result of type std::weak_ordering, even if operator<=> is unavailable
(customization point object)

[edit]

compare_partial_order_fallback

(C++20)

performs 3-way comparison and produces a result of type std::partial_ordering, even if operator<=> is unavailable
(customization point object)

[edit]

Functions

is_eqis_neqis_ltis_lteqis_gtis_gteq

(C++20)

named comparison functions
(function)

[edit]

Synopsis

// all freestandingnamespacestd{// comparison category typesclasspartial_ordering;classweak_ordering;classstrong_ordering;// named comparison functionsconstexprboolis_eq(partial_orderingcmp)noexcept{returncmp==0;}constexprboolis_neq(partial_orderingcmp)noexcept{returncmp!=0;}constexprboolis_lt(partial_orderingcmp)noexcept{returncmp<0;}constexprboolis_lteq(partial_orderingcmp)noexcept{returncmp<=0;}constexprboolis_gt(partial_orderingcmp)noexcept{returncmp>0;}constexprboolis_gteq(partial_orderingcmp)noexcept{returncmp>=0;}// common comparison category typetemplate<class...Ts>structcommon_comparison_category{usingtype=/* see description */;};template<class...Ts>usingcommon_comparison_category_t=common_comparison_category<Ts...>::type;// concept three_way_comparabletemplate<classT,classCat=partial_ordering>conceptthree_way_comparable=/* see description */;template<classT,classU,classCat=partial_ordering>conceptthree_way_comparable_with=/* see description */;// result of three-way comparisontemplate<classT,classU=T>structcompare_three_way_result;template<classT,classU=T>usingcompare_three_way_result_t=compare_three_way_result<T,U>::type;// class compare_three_waystructcompare_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 */;}// type orderingtemplate<classT,classU>structtype_order;template<classT,classU>constexprstrong_orderingtype_order_v=type_order<T,U>::value;}Concept

three_way_comparable

namespacestd{template<classT,classCat>concept/*compares-as*/=// exposition-onlysame_as<common_comparison_category_t<T,Cat>,Cat>;template<classT,classU>concept/*partially-ordered-with*/=// exposition-onlyrequires(constremove_reference_t<T>&t,constremove_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<classT,classCat=partial_ordering>conceptthree_way_comparable=/*weakly-equality-comparable-with*/<T,T>&&/*partially-ordered-with*/<T,T>&&requires(constremove_reference_t<T>&a,constremove_reference_t<T>&b){{a<=>b}->/*compares-as*/<Cat>;};}Concept

three_way_comparable_with

namespacestd{template<classT,classU,classCat=partial_ordering>conceptthree_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<constremove_reference_t<T>&,constremove_reference_t<U>&>,Cat>&&/*weakly-equality-comparable-with*/<T,U>&&/*partially-ordered-with*/<T,U>&&requires(constremove_reference_t<T>&t,constremove_reference_t<U>&u){{t<=>u}->/*compares-as*/<Cat>;{u<=>t}->/*compares-as*/<Cat>;};}Class std::partial_ordering

namespacestd{classpartial_ordering{int/*value*/;// exposition-onlybool/*is-ordered*/;// exposition-only// exposition-only constructorsconstexprexplicitpartial_ordering(/*ord*/v)noexcept:/*value*/(int(v)),/*is-ordered*/(true){}// exposition-onlyconstexprexplicitpartial_ordering(/*ncmp*/v)noexcept:/*value*/(int(v)),/*is-ordered*/(false){}// exposition-onlypublic:// valid valuesstaticconstpartial_orderingless;staticconstpartial_orderingequivalent;staticconstpartial_orderinggreater;staticconstpartial_orderingunordered;// comparisonsfriendconstexprbooloperator==(partial_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator==(partial_orderingv,partial_orderingw)noexcept=default;friendconstexprbooloperator<(partial_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator>(partial_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator<=(partial_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator>=(partial_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator<(/* unspecified */,partial_orderingv)noexcept;friendconstexprbooloperator>(/* unspecified */,partial_orderingv)noexcept;friendconstexprbooloperator<=(/* unspecified */,partial_orderingv)noexcept;friendconstexprbooloperator>=(/* unspecified */,partial_orderingv)noexcept;friendconstexprpartial_orderingoperator<=>(partial_orderingv,/* unspecified */)noexcept;friendconstexprpartial_orderingoperator<=>(/* unspecified */,partial_orderingv)noexcept;};// valid values' definitionsinlineconstexprpartial_orderingpartial_ordering::less(/*ord*/::/*less*/);inlineconstexprpartial_orderingpartial_ordering::equivalent(/*ord*/::/*equivalent*/);inlineconstexprpartial_orderingpartial_ordering::greater(/*ord*/::/*greater*/);inlineconstexprpartial_orderingpartial_ordering::unordered(/*ncmp*/::/*unordered*/);}Class std::weak_ordering

namespacestd{classweak_ordering{int/*value*/;// exposition-only// exposition-only constructorsconstexprexplicitweak_ordering(/*ord*/v)noexcept:/*value*/(int(v)){}// exposition-onlypublic:// valid valuesstaticconstweak_orderingless;staticconstweak_orderingequivalent;staticconstweak_orderinggreater;// conversionsconstexproperatorpartial_ordering()constnoexcept;// comparisonsfriendconstexprbooloperator==(weak_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator==(weak_orderingv,weak_orderingw)noexcept=default;friendconstexprbooloperator<(weak_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator>(weak_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator<=(weak_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator>=(weak_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator<(/* unspecified */,weak_orderingv)noexcept;friendconstexprbooloperator>(/* unspecified */,weak_orderingv)noexcept;friendconstexprbooloperator<=(/* unspecified */,weak_orderingv)noexcept;friendconstexprbooloperator>=(/* unspecified */,weak_orderingv)noexcept;friendconstexprweak_orderingoperator<=>(weak_orderingv,/* unspecified */)noexcept;friendconstexprweak_orderingoperator<=>(/* unspecified */,weak_orderingv)noexcept;};// valid values' definitionsinlineconstexprweak_orderingweak_ordering::less(/*ord*/::/*less*/);inlineconstexprweak_orderingweak_ordering::equivalent(/*ord*/::/*equivalent*/);inlineconstexprweak_orderingweak_ordering::greater(/*ord*/::/*greater*/);}Class std::strong_ordering

namespacestd{classstrong_ordering{int/*value*/;// exposition-only// exposition-only constructorsconstexprexplicitstrong_ordering(/*ord*/v)noexcept:/*value*/(int(v)){}// exposition-onlypublic:// valid valuesstaticconststrong_orderingless;staticconststrong_orderingequal;staticconststrong_orderingequivalent;staticconststrong_orderinggreater;// conversionsconstexproperatorpartial_ordering()constnoexcept;constexproperatorweak_ordering()constnoexcept;// comparisonsfriendconstexprbooloperator==(strong_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator==(strong_orderingv,strong_orderingw)noexcept=default;friendconstexprbooloperator<(strong_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator>(strong_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator<=(strong_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator>=(strong_orderingv,/* unspecified */)noexcept;friendconstexprbooloperator<(/* unspecified */,strong_orderingv)noexcept;friendconstexprbooloperator>(/* unspecified */,strong_orderingv)noexcept;friendconstexprbooloperator<=(/* unspecified */,strong_orderingv)noexcept;friendconstexprbooloperator>=(/* unspecified */,strong_orderingv)noexcept;friendconstexprstrong_orderingoperator<=>(strong_orderingv,/* unspecified */)noexcept;friendconstexprstrong_orderingoperator<=>(/* unspecified */,strong_orderingv)noexcept;};// valid values' definitionsinlineconstexprstrong_orderingstrong_ordering::less(/*ord*/::/*less*/);inlineconstexprstrong_orderingstrong_ordering::equal(/*ord*/::/*equal*/);inlineconstexprstrong_orderingstrong_ordering::equivalent(/*ord*/::/*equivalent*/);inlineconstexprstrong_orderingstrong_ordering::greater(/*ord*/::/*greater*/);}Class std::compare_three_way

namespacestd{structcompare_three_way{template<classT,classU>constexprautooperator()(T&&t,U&&u)const;usingis_transparent=/* unspecified */;};}Class std::type_order

namespacestd{template<classT,classU>structtype_order{staticconstexprstrong_orderingvalue=/*TYPE-ORDER*/(T,U);usingvalue_type=strong_ordering;constexproperatorvalue_type()constnoexcept{returnvalue;}constexprvalue_typeoperator()()constnoexcept{returnvalue;}};}See also