From 415fd4d2ecd8d846f15f1e4dd1c9865f85a15347 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sun, 25 Apr 2021 22:27:54 +0430 Subject: [PATCH] AK: Make DistinctNumeric constexpr-capable --- AK/DistinctNumeric.h | 74 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/AK/DistinctNumeric.h b/AK/DistinctNumeric.h index 44af5a11543..f6c2c67dbe0 100644 --- a/AK/DistinctNumeric.h +++ b/AK/DistinctNumeric.h @@ -52,48 +52,48 @@ class DistinctNumeric { using Self = DistinctNumeric; public: - DistinctNumeric() + constexpr DistinctNumeric() { } - DistinctNumeric(T value) + constexpr DistinctNumeric(T value) : m_value { value } { } - const T& value() const { return m_value; } + constexpr const T& value() const { return m_value; } // Always implemented: identity. - bool operator==(const Self& other) const + constexpr bool operator==(const Self& other) const { return this->m_value == other.m_value; } - bool operator!=(const Self& other) const + constexpr bool operator!=(const Self& other) const { return this->m_value != other.m_value; } // Only implemented when `Incr` is true: - Self& operator++() + constexpr Self& operator++() { static_assert(Incr, "'++a' is only available for DistinctNumeric types with 'Incr'."); this->m_value += 1; return *this; } - Self operator++(int) + constexpr Self operator++(int) { static_assert(Incr, "'a++' is only available for DistinctNumeric types with 'Incr'."); Self ret = this->m_value; this->m_value += 1; return ret; } - Self& operator--() + constexpr Self& operator--() { static_assert(Incr, "'--a' is only available for DistinctNumeric types with 'Incr'."); this->m_value -= 1; return *this; } - Self operator--(int) + constexpr Self operator--(int) { static_assert(Incr, "'a--' is only available for DistinctNumeric types with 'Incr'."); Self ret = this->m_value; @@ -102,22 +102,22 @@ public: } // Only implemented when `Cmp` is true: - bool operator>(const Self& other) const + constexpr bool operator>(const Self& other) const { static_assert(Cmp, "'a>b' is only available for DistinctNumeric types with 'Cmp'."); return this->m_value > other.m_value; } - bool operator<(const Self& other) const + constexpr bool operator<(const Self& other) const { static_assert(Cmp, "'am_value < other.m_value; } - bool operator>=(const Self& other) const + constexpr bool operator>=(const Self& other) const { static_assert(Cmp, "'a>=b' is only available for DistinctNumeric types with 'Cmp'."); return this->m_value >= other.m_value; } - bool operator<=(const Self& other) const + constexpr bool operator<=(const Self& other) const { static_assert(Cmp, "'a<=b' is only available for DistinctNumeric types with 'Cmp'."); return this->m_value <= other.m_value; @@ -125,7 +125,7 @@ public: // 'operator<=>' cannot be implemented. See class comment. // Only implemented when `bool` is true: - bool operator!() const + constexpr bool operator!() const { static_assert(Bool, "'!a' is only available for DistinctNumeric types with 'Bool'."); return !this->m_value; @@ -136,39 +136,39 @@ public: // `operator bool() const` would defy the entire point of this class. // Only implemented when `Flags` is true: - Self operator~() const + constexpr Self operator~() const { static_assert(Flags, "'~a' is only available for DistinctNumeric types with 'Flags'."); return ~this->m_value; } - Self operator&(const Self& other) const + constexpr Self operator&(const Self& other) const { static_assert(Flags, "'a&b' is only available for DistinctNumeric types with 'Flags'."); return this->m_value & other.m_value; } - Self operator|(const Self& other) const + constexpr Self operator|(const Self& other) const { static_assert(Flags, "'a|b' is only available for DistinctNumeric types with 'Flags'."); return this->m_value | other.m_value; } - Self operator^(const Self& other) const + constexpr Self operator^(const Self& other) const { static_assert(Flags, "'a^b' is only available for DistinctNumeric types with 'Flags'."); return this->m_value ^ other.m_value; } - Self& operator&=(const Self& other) + constexpr Self& operator&=(const Self& other) { static_assert(Flags, "'a&=b' is only available for DistinctNumeric types with 'Flags'."); this->m_value &= other.m_value; return *this; } - Self& operator|=(const Self& other) + constexpr Self& operator|=(const Self& other) { static_assert(Flags, "'a|=b' is only available for DistinctNumeric types with 'Flags'."); this->m_value |= other.m_value; return *this; } - Self& operator^=(const Self& other) + constexpr Self& operator^=(const Self& other) { static_assert(Flags, "'a^=b' is only available for DistinctNumeric types with 'Flags'."); this->m_value ^= other.m_value; @@ -177,23 +177,23 @@ public: // Only implemented when `Shift` is true: // TODO: Should this take `int` instead? - Self operator<<(const Self& other) const + constexpr Self operator<<(const Self& other) const { static_assert(Shift, "'a<m_value << other.m_value; } - Self operator>>(const Self& other) const + constexpr Self operator>>(const Self& other) const { static_assert(Shift, "'a>>b' is only available for DistinctNumeric types with 'Shift'."); return this->m_value >> other.m_value; } - Self& operator<<=(const Self& other) + constexpr Self& operator<<=(const Self& other) { static_assert(Shift, "'a<<=b' is only available for DistinctNumeric types with 'Shift'."); this->m_value <<= other.m_value; return *this; } - Self& operator>>=(const Self& other) + constexpr Self& operator>>=(const Self& other) { static_assert(Shift, "'a>>=b' is only available for DistinctNumeric types with 'Shift'."); this->m_value >>= other.m_value; @@ -201,66 +201,66 @@ public: } // Only implemented when `Arith` is true: - Self operator+(const Self& other) const + constexpr Self operator+(const Self& other) const { static_assert(Arith, "'a+b' is only available for DistinctNumeric types with 'Arith'."); return this->m_value + other.m_value; } - Self operator-(const Self& other) const + constexpr Self operator-(const Self& other) const { static_assert(Arith, "'a-b' is only available for DistinctNumeric types with 'Arith'."); return this->m_value - other.m_value; } - Self operator+() const + constexpr Self operator+() const { static_assert(Arith, "'+a' is only available for DistinctNumeric types with 'Arith'."); return +this->m_value; } - Self operator-() const + constexpr Self operator-() const { static_assert(Arith, "'-a' is only available for DistinctNumeric types with 'Arith'."); return -this->m_value; } - Self operator*(const Self& other) const + constexpr Self operator*(const Self& other) const { static_assert(Arith, "'a*b' is only available for DistinctNumeric types with 'Arith'."); return this->m_value * other.m_value; } - Self operator/(const Self& other) const + constexpr Self operator/(const Self& other) const { static_assert(Arith, "'a/b' is only available for DistinctNumeric types with 'Arith'."); return this->m_value / other.m_value; } - Self operator%(const Self& other) const + constexpr Self operator%(const Self& other) const { static_assert(Arith, "'a%b' is only available for DistinctNumeric types with 'Arith'."); return this->m_value % other.m_value; } - Self& operator+=(const Self& other) + constexpr Self& operator+=(const Self& other) { static_assert(Arith, "'a+=b' is only available for DistinctNumeric types with 'Arith'."); this->m_value += other.m_value; return *this; } - Self& operator-=(const Self& other) + constexpr Self& operator-=(const Self& other) { static_assert(Arith, "'a+=b' is only available for DistinctNumeric types with 'Arith'."); this->m_value += other.m_value; return *this; } - Self& operator*=(const Self& other) + constexpr Self& operator*=(const Self& other) { static_assert(Arith, "'a*=b' is only available for DistinctNumeric types with 'Arith'."); this->m_value *= other.m_value; return *this; } - Self& operator/=(const Self& other) + constexpr Self& operator/=(const Self& other) { static_assert(Arith, "'a/=b' is only available for DistinctNumeric types with 'Arith'."); this->m_value /= other.m_value; return *this; } - Self& operator%=(const Self& other) + constexpr Self& operator%=(const Self& other) { static_assert(Arith, "'a%=b' is only available for DistinctNumeric types with 'Arith'."); this->m_value %= other.m_value;