From 64a242261e0aa3bb853453e2d1c472ce8f558cda Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 2 Jan 2023 00:31:58 -0700 Subject: [PATCH] AK: Reimplement DistinctNumeric comparison operators using operator<=> Unlike what the class comment says, it's actually valid to return int from this operator and treat it like a "normal" C-like compare method. --- AK/DistinctNumeric.h | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/AK/DistinctNumeric.h b/AK/DistinctNumeric.h index a106cb9a033..7cb9f2e42e6 100644 --- a/AK/DistinctNumeric.h +++ b/AK/DistinctNumeric.h @@ -24,7 +24,7 @@ namespace AK { * - No matter the values of these, `DistinctNumeric` always implements `==` and `!=`. * - If `Arithmetic` is present, then `a+b`, `a-b`, `+a`, `-a`, `a*b`, `a/b`, `a%b`, and the respective `a_=b` versions are implemented. * - If `CastToBool` is present, then `!a`, `a&&b`, and `a||b` are implemented (but not `operator bool()`, because of overzealous integer promotion rules). - * - If `Comparison` is present, then `a>b`, `a=b`, and `a<=b` are implemented. + * - If `Comparison` is present, then `a>b`, `a=b`, and `a<=b` are implemented via operator<=> * - If `Flags` is present, then `~a`, `a&b`, `a|b`, `a^b`, `a&=b`, `a|=b`, and `a^=b` are implemented. * - If `Increment` is present, then `++a`, `a++`, `--a`, and `a--` are implemented. * - If `Shift` is present, then `a<>b`, `a<<=b`, `a>>=b` are implemented. @@ -38,9 +38,6 @@ namespace AK { * I intentionally decided against overloading `&a` because these shall remain * numeric types. * - * The C++20 `operator<=>` would require, among other things `std::weak_equality`. - * Since we do not have that, it cannot be implemented. - * * The are many operators that do not work on `int`, so I left them out: * `a[b]`, `*a`, `a->b`, `a.b`, `a->*b`, `a.*b`. * @@ -144,27 +141,12 @@ public: } // Only implemented when `Comparison` is true: - constexpr bool operator>(Self const& other) const + constexpr int operator<=>(Self const& other) const { - static_assert(options.comparisons, "'a>b' is only available for DistinctNumeric types with 'Comparison'."); - return this->m_value > other.m_value; + static_assert(options.comparisons, "'a<=>b' is only available for DistinctNumeric types with 'Comparison'."); + return this->m_value > other.m_value ? 1 : this->m_value < other.m_value ? -1 + : 0; } - constexpr bool operator<(Self const& other) const - { - static_assert(options.comparisons, "'am_value < other.m_value; - } - constexpr bool operator>=(Self const& other) const - { - static_assert(options.comparisons, "'a>=b' is only available for DistinctNumeric types with 'Comparison'."); - return this->m_value >= other.m_value; - } - constexpr bool operator<=(Self const& other) const - { - static_assert(options.comparisons, "'a<=b' is only available for DistinctNumeric types with 'Comparison'."); - return this->m_value <= other.m_value; - } - // 'operator<=>' cannot be implemented. See class comment. // Only implemented when `CastToBool` is true: constexpr bool operator!() const