refactor: Prefer <=> over multiple relational operators

Define `operator<=>` in classes that have all of `<`, `<=`, `>`, `>=`.
This commit is contained in:
Daniel Pfeifer
2025-11-03 11:50:18 +01:00
parent 5a0f49bd26
commit 48840bfc2d
4 changed files with 7 additions and 28 deletions

View File

@@ -72,10 +72,7 @@ public:
iterator operator-(size_type n) const { return iterator(ptr - n); }
iterator& operator-=(size_type n) { ptr -= n; return *this; }
bool operator==(iterator x) const { return ptr == x.ptr; }
bool operator>=(iterator x) const { return ptr >= x.ptr; }
bool operator<=(iterator x) const { return ptr <= x.ptr; }
bool operator>(iterator x) const { return ptr > x.ptr; }
bool operator<(iterator x) const { return ptr < x.ptr; }
auto operator<=>(iterator x) const { return ptr <=> x.ptr; }
};
class const_iterator {
@@ -103,10 +100,7 @@ public:
const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
bool operator==(const_iterator x) const { return ptr == x.ptr; }
bool operator>=(const_iterator x) const { return ptr >= x.ptr; }
bool operator<=(const_iterator x) const { return ptr <= x.ptr; }
bool operator>(const_iterator x) const { return ptr > x.ptr; }
bool operator<(const_iterator x) const { return ptr < x.ptr; }
auto operator<=>(const_iterator x) const { return ptr <=> x.ptr; }
};
private:

View File

@@ -270,16 +270,10 @@ public:
}
inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
inline auto operator<=>(const int64_t& rhs) const { return m_value <=> rhs; }
inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
inline auto operator<=>(const CScriptNum& rhs) const { return operator<=>(rhs.m_value); }
inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}

View File

@@ -61,16 +61,10 @@ public:
}
inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
inline auto operator<=>(const int64_t& rhs) const { return m_value <=> rhs; }
inline bool operator==(const CScriptNum10& rhs) const { return operator==(rhs.m_value); }
inline bool operator<=(const CScriptNum10& rhs) const { return operator<=(rhs.m_value); }
inline bool operator< (const CScriptNum10& rhs) const { return operator< (rhs.m_value); }
inline bool operator>=(const CScriptNum10& rhs) const { return operator>=(rhs.m_value); }
inline bool operator> (const CScriptNum10& rhs) const { return operator> (rhs.m_value); }
inline auto operator<=>(const CScriptNum10& rhs) const { return operator<=>(rhs.m_value); }
inline CScriptNum10 operator+( const int64_t& rhs) const { return CScriptNum10(m_value + rhs);}
inline CScriptNum10 operator-( const int64_t& rhs) const { return CScriptNum10(m_value - rhs);}

View File

@@ -99,10 +99,7 @@ class bitdeque
friend Iterator operator+(Iterator x, difference_type dist) { x += dist; return x; }
friend Iterator operator+(difference_type dist, Iterator x) { x += dist; return x; }
friend Iterator operator-(Iterator x, difference_type dist) { x -= dist; return x; }
friend bool operator<(const Iterator& x, const Iterator& y) { return std::tie(x.m_it, x.m_bitpos) < std::tie(y.m_it, y.m_bitpos); }
friend bool operator>(const Iterator& x, const Iterator& y) { return std::tie(x.m_it, x.m_bitpos) > std::tie(y.m_it, y.m_bitpos); }
friend bool operator<=(const Iterator& x, const Iterator& y) { return std::tie(x.m_it, x.m_bitpos) <= std::tie(y.m_it, y.m_bitpos); }
friend bool operator>=(const Iterator& x, const Iterator& y) { return std::tie(x.m_it, x.m_bitpos) >= std::tie(y.m_it, y.m_bitpos); }
friend auto operator<=>(const Iterator& x, const Iterator& y) { return std::tie(x.m_it, x.m_bitpos) <=> std::tie(y.m_it, y.m_bitpos); }
friend bool operator==(const Iterator& x, const Iterator& y) { return x.m_it == y.m_it && x.m_bitpos == y.m_bitpos; }
reference operator*() const { return (*m_it)[m_bitpos]; }
reference operator[](difference_type pos) const { return *(*this + pos); }