std::compar...er_fallback(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 == u and t < u are both well-formed and each of decltype(t == u) and decltype(t < u) models boolean-testable, except that t and u are 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 to bool. Correct behavior: constraints strengthened (P2167R3).

πŸ‘‘ See also πŸ‘‘

  • weak_order (C++20) β€” performs 3-way comparison and produces a result of type std::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