From cppreference.com
Defined in header
inlinenamespace/* unspecified */{inlineconstexpr/* unspecified */compare_weak_order_fallback=/* unspecified */;}(since C++20)Call signature
template<classT,classU>requires/* see below */constexprstd::weak_orderingcompare_weak_order_fallback(T&&t,U&&u)noexcept(/* see below */);(since C++20)Performs three-way comparison on
t and u and produces a result of type
, even if the operator <=> is unavailable.
If std::decay_t<T> and std::decay_t<U> are the same type, std::compare_weak_order_fallback(t,u) is
to:
std::weak_order(t,u), if it is a well-formed expression; otherwise,
t==u?std::weak_ordering::equivalent:
t<u?std::weak_ordering::less:
std::weak_ordering::greater, if the expressions t==u and t<u are both well-formed and each of decltype(t==u) and decltype(t<u) models
, except that t and u are evaluated only once.
In all other cases, std::compare_weak_order_fallback(t,u) is ill-formed, which can result in
when it appears in the immediate context of a template instantiation.
Customization point objects
The name std::compare_weak_order_fallback denotes a customization point object, which is a const
of a
class type. See
for details.
Example
Run this code
#include<compare>#include<iostream>// does not support <=>structRational_1{intnum;intden;// > 0};inlineconstexprbooloperator<(Rational_1lhs,Rational_1rhs){returnlhs.num*rhs.den<rhs.num*lhs.den;}inlineconstexprbooloperator==(Rational_1lhs,Rational_1rhs){returnlhs.num*rhs.den==rhs.num*lhs.den;}// supports <=>structRational_2{intnum;intden;// > 0};inlineconstexprstd::weak_orderingoperator<=>(Rational_2lhs,Rational_2rhs){returnlhs.num*rhs.den<=>rhs.num*lhs.den;}inlineconstexprbooloperator==(Rational_2lhs,Rational_2rhs){returnlhs<=>rhs==0;}voidprint(intid,std::weak_orderingvalue){std::cout<<id<<") ";if(value==0)std::cout<<"equal\n";elseif(value<0)std::cout<<"less\n";elsestd::cout<<"greater\n";}intmain(){Rational_1a{1,2},b{3,4};// print(0, a <=> b); // does not workprint(1,std::compare_weak_order_fallback(a,b));// works, defaults to < and ==Rational_2c{6,5},d{8,7};print(2,c<=>d);// worksprint(3,std::compare_weak_order_fallback(c,d));// worksRational_2e{2,3},f{4,6};print(4,e<=>f);// worksprint(5,std::compare_weak_order_fallback(e,f));// works}Output:
1) less 2) greater 3) greater 4) equal 5) equal Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
(
) C++20 the fallback mechanism only required
return types to be convertible to boolconstraints strengthened See also
(C++20)
performs 3-way comparison and produces a result of type std::weak_ordering
(customization point object)