From 48840bfc2d7beeac0ddf56a3c26b243156ec8936 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Mon, 3 Nov 2025 11:50:18 +0100 Subject: [PATCH] refactor: Prefer `<=>` over multiple relational operators Define `operator<=>` in classes that have all of `<`, `<=`, `>`, `>=`. --- src/prevector.h | 10 ++-------- src/script/script.h | 10 ++-------- src/test/scriptnum10.h | 10 ++-------- src/util/bitdeque.h | 5 +---- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/src/prevector.h b/src/prevector.h index 2c794d71041..d4d90c7350f 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -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: diff --git a/src/script/script.h b/src/script/script.h index 633b5609c05..b06be9c975d 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -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);} diff --git a/src/test/scriptnum10.h b/src/test/scriptnum10.h index bf708aacd6e..402606714d4 100644 --- a/src/test/scriptnum10.h +++ b/src/test/scriptnum10.h @@ -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);} diff --git a/src/util/bitdeque.h b/src/util/bitdeque.h index 2d21c8b3b39..21934e02dad 100644 --- a/src/util/bitdeque.h +++ b/src/util/bitdeque.h @@ -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); }