std::compare_three_way(3)
π std::compare_three_way β the C++ Standard Library page, but make it cute
okay so bestie, this one is std::compare_three_way, and it lives in the C++ Standard Library (the std:: namespace, obviously). it's a little function object that does the three-way comparison thing, and honestly? it's giving "quietly iconic." let me walk you through it π«Ά
β¨ SYNOPSIS β¨
so it's defined in two headers, yes two, don't @ me:
Defined in header <compare>
Defined in header <functional>
and the struct itself:
struct compare_three_way; (since C++20)
like the title says, this is a Function object for performing comparisons π and here's the cute part β it deduces the parameter types and the return type of the function call operator. it just knows. main character energy fr.
π MEMBER FUNCTIONS π
operator()β obtains the result of three-way comparison on both arguments (public member function)
π std::compare_three_way::operator() π
here's the signature, exactly as written:
template< class T, class U >
constexpr auto operator()( T&& t, U&& u ) const;
okay so, given the expression std::forward<T>(t) <=> std::forward<U>(u) as expr:
- if expr results in a call to built-in
operator<=>comparing pointers, given the composite pointer type oftanduasP:- it compares the two converted pointers (of type
P) in the implementation-defined strict total order over pointers:- if
tprecedesu, returnsstd::strong_ordering::less. - if
uprecedest, returnsstd::strong_ordering::greater. - otherwise, returns
std::strong_ordering::equal.
- if
- if the conversion sequence from
TtoPor the conversion sequence fromUtoPis not equality-preserving, the behavior is undefined π (yes, undefined. be careful bestie.)
- it compares the two converted pointers (of type
- otherwise:
- returns the result of expr.
- if
std::three_way_comparable_with<T, U>is not modeled, the behavior is undefined π
also, important vibe check: this overload participates in overload resolution only if std::three_way_comparable_with<T, U> is satisfied.
π EXAMPLE π
run this code, it's so good:
#include <compare>
#include <iostream>
struct Rational
{
int num;
int den; // > 0
// Although the comparison X <=> Y will work, a direct call
// to std::compare_three_way{}(X, Y) requires the operator==
// be defined, to satisfy the std::three_way_comparable_with.
constexpr bool operator==(Rational const&) const = default;
};
constexpr std::weak_ordering operator<=>(Rational lhs, Rational rhs)
{
return lhs.num * rhs.den <=> rhs.num * lhs.den;
}
void print(std::weak_ordering value)
{
value < 0 ? std::cout << "less\n" :
value > 0 ? std::cout << "greater\n" :
std::cout << "equal\n";
}
int main()
{
Rational a{6, 5};
Rational b{8, 7};
print(a <=> b);
print(std::compare_three_way{}(a, b));
}
Output:
greater
greater
both lines match, we love consistency β¨
ngl the comment in that example is the whole tea βX <=> Yworks fine, but callingstd::compare_three_way{}(X, Y)directly needsoperator==defined sostd::three_way_comparable_withis happy. iykyk π§
π¦ DEFECT REPORTS π¦
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (yes, they went back and fixed the past, iconic behavior π)
- LWG 3530 β applied to C++20 β behavior as published: syntactic checks were relaxed while comparing pointers β correct behavior: only semantic requirements are relaxed
β SEE ALSO β
ranges::equal_to(C++20) β constrained function object implementingx == y(class)ranges::not_equal_to(C++20) β constrained function object implementingx != y(class)ranges::less(C++20) β constrained function object implementingx < y(class)ranges::greater(C++20) β constrained function object implementingx > y(class)ranges::less_equal(C++20) β constrained function object implementingx <= y(class)ranges::greater_equal(C++20) β constrained function object implementingx >= y(class)
okay that's the whole page!! every flag, every warning, every undefined-behavior jumpscare, all present and accounted for π keep slaying those comparisons, bestie. mwah π
source: http://cppreference.com β version 2024.06.10 β page: std::compare_three_way(3) π¦β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms