AK: Make DistinctNumeric constexpr-capable

This commit is contained in:
Ali Mohammad Pur 2021-04-25 22:27:54 +04:30 committed by Andreas Kling
parent 93f03c73d9
commit 415fd4d2ec
Notes: sideshowbarker 2024-07-18 18:43:02 +09:00

View file

@ -52,48 +52,48 @@ class DistinctNumeric {
using Self = DistinctNumeric<T, X, Incr, Cmp, Bool, Flags, Shift, Arith>;
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, "'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;
}
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<<b' is only available for DistinctNumeric types with 'Shift'.");
return this->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;