std::compar...er_fallback(3)
π
std::compare_weak_order_fallback(3) π
okay so real talk, this whole page lives in the C++ Standard Libary manual (yes that's literally how it's spelled up top, don't come for me, i just report what i see π) and it's about one very specific little guy. let's get into it π«Ά
β¨ NAME β¨
std::compare_weak_order_fallbackβ that's it, that's the name.std::compare_weak_order_fallback. no notes. π
πΈ Synopsis πΈ
Defined in header <compare>
inline namespace /* unspecified */ {
inline constexpr /* unspecified */
(since C++20)
compare_weak_order_fallback = /* unspecified */;
}
π Call signature π
template< class T, class U >
requires /* see below */
constexpr std::weak_ordering
(since C++20)
compare_weak_order_fallback( T&& t, U&& u ) noexcept(/* see below
*/);
okay so what does it DO π it performs a three-way comparison on the subexpressions t and u, and it hands you back a result of type std::weak_ordering β even if operator <=> is unavailable. that's the whole point of the "fallback" in the name, bestie, it's the safety net π§
here's the tea on how it behaves π
If std::decay_t<T> and std::decay_t<U> are the same type, then std::compare_weak_order_fallback(t, u) is expression-equivalent to:
std::weak_order(t, u), if it is a well-formed expression; otherwise,- the following, if the expressions
t == uandt < uare both well-formed and each ofdecltype(t == u)anddecltype(t < u)modelsboolean-testable, except thattanduare evaluated only once:
t == u ? std::weak_ordering::equivalent :
t < u ? std::weak_ordering::less :
std::weak_ordering::greater
β οΈ note that evaluated only once clause, okay? that's not a vibe, that's a guarantee. do not sleep on it.
In all other cases, std::compare_weak_order_fallback(t, u) is ill-formed, which can result in substitution failure when it appears in the immediate context of a template instantiation. π that's the dramatic part. it justβ¦ doesn't exist.
π₯ Example π₯
// Run this code
#include <compare>
#include <iostream>
// does not support <=>
struct Rational_1
{
int num;
int den; // > 0
};
inline constexpr bool operator<(Rational_1 lhs, Rational_1 rhs)
{
return lhs.num * rhs.den < rhs.num * lhs.den;
}
inline constexpr bool operator==(Rational_1 lhs, Rational_1 rhs)
{
return lhs.num * rhs.den == rhs.num * lhs.den;
}
// supports <=>
struct Rational_2
{
int num;
int den; // > 0
};
inline constexpr std::weak_ordering operator<=>(Rational_2 lhs,
Rational_2 rhs)
{
return lhs.num * rhs.den <=> rhs.num * lhs.den;
}
inline constexpr bool operator==(Rational_2 lhs, Rational_2 rhs)
{
return lhs <=> rhs == 0;
}
void print(int id, std::weak_ordering value)
{
std::cout << id << ") ";
if (value == 0)
std::cout << "equal\n";
else if (value < 0)
std::cout << "less\n";
else
std::cout << "greater\n";
}
int main()
{
Rational_1 a{1, 2}, b{3, 4};
// print(0, a <=> b); // does not work
print(1, std::compare_weak_order_fallback(a, b)); // works,
defaults to < and ==
Rational_2 c{6, 5}, d{8, 7};
print(2, c <=> d); // works
print(3, std::compare_weak_order_fallback(c, d)); // works
Rational_2 e{2, 3}, f{4, 6};
print(4, e <=> f); // works
print(5, std::compare_weak_order_fallback(e, f)); // works
}
π Output π
1) less
2) greater
3) greater
4) equal
5) equal
like, look at Rational_1 out here with no <=> at all and the fallback still ate. π₯ iconic behavior honestly.
π¦ Defect reports π¦
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (retroactively!! time travel!! β°β¨)
LWG 2114β Applied to C++20. Behavior as published: the fallback mechanism only required return types to be convertible tobool. Correct behavior: constraints strengthened (P2167R3).
π See also π
weak_order(C++20) β performs 3-way comparison and produces a result of typestd::weak_ordering(customization point object).
okay that's the whole page, you're so welcome, go write some <=> operators π«Άπβ¨
page pulled from http://cppreference.com, dated 2024.06.10, and it's the std::compare_weak_order_fallback(3) page specifically β mwah, love that for us π
π
π¦ 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