From 3dc6b37bae9e41987cb8947da5c91aa2b55a4185 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 4 Sep 2024 11:21:51 +0200 Subject: [PATCH] QVersionNumber: make iterator comparison non-noexcept again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When these iterators were originally added¹, their relational operators, following the Lakos Rule, were not marked as noexcept because they have preconditions (lhs and rhs must come from the same QVersionNumber object). d292648d0bbac50388dae035dc34782accb3807f then added the noexcept, disregarding the Lakos Rule. Until the ongoing discussion regarding the Lakos Rule on the project mailing-list² is concluded, don't add noexcept. We can always add it in later, but we can't remove it once released. Requires manually working around the missing Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE_NON_NOEXCEPT macro that we don't want to add if there's no other user. ¹ 2188ca2c5df6f21a953c002edbe5b2d2cc2c2d2c ² thread starting at https://lists.qt-project.org/pipermail/development/2024-August/045544.html Change-Id: Ie88326519673166afe8cb44267c23944b27c68d2 Reviewed-by: Ivan Solovev (cherry picked from commit a84b79df0fb8a92b17a39bbf706714e93de9d6fc) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/qversionnumber.h | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/corelib/tools/qversionnumber.h b/src/corelib/tools/qversionnumber.h index b6cfcf7debf..90cde6f0a8d 100644 --- a/src/corelib/tools/qversionnumber.h +++ b/src/corelib/tools/qversionnumber.h @@ -201,11 +201,35 @@ class QVersionNumber friend class QVersionNumber; explicit constexpr It(const QVersionNumber *vn, qsizetype idx) noexcept : v(vn), i(idx) {} - friend constexpr bool comparesEqual(const It &lhs, const It &rhs) noexcept + friend constexpr bool comparesEqual(const It &lhs, const It &rhs) { Q_ASSERT(lhs.v == rhs.v); return lhs.i == rhs.i; } - friend constexpr Qt::strong_ordering compareThreeWay(const It &lhs, const It &rhs) noexcept + friend constexpr Qt::strong_ordering compareThreeWay(const It &lhs, const It &rhs) { Q_ASSERT(lhs.v == rhs.v); return Qt::compareThreeWay(lhs.i, rhs.i); } - Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(It) + // macro variant does not exist: Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE_NON_NOEXCEPT(It) + friend constexpr bool operator==(It lhs, It rhs) { + return comparesEqual(lhs, rhs); + } +#ifdef __cpp_lib_three_way_comparison + friend constexpr std::strong_ordering operator<=>(It lhs, It rhs) { + return compareThreeWay(lhs, rhs); + } +#else + friend constexpr bool operator!=(It lhs, It rhs) { + return !operator==(lhs, rhs); + } + friend constexpr bool operator<(It lhs, It rhs) { + return is_lt(compareThreeWay(lhs, rhs)); + } + friend constexpr bool operator<=(It lhs, It rhs) { + return is_lteq(compareThreeWay(lhs, rhs)); + } + friend constexpr bool operator>(It lhs, It rhs) { + return is_gt(compareThreeWay(lhs, rhs)); + } + friend constexpr bool operator>=(It lhs, It rhs) { + return is_gteq(compareThreeWay(lhs, rhs)); + } +#endif public: // Rule Of Zero applies