From 48cc0f9612983d5f2f1614756aebb119e2c3051c Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 20 Dec 2024 13:11:13 +0100 Subject: [PATCH] Refactor hasCompareThreeWay check Use structs derived from std::false_type and std::true_type instead of boolean constants. This allows us to make use of std::conjunction_v and std::disjunciton_v in the conditions, thus making use of short-circuit evaluation and saving unnecessary template instantiations. Still keep the constexpr bool *_v constant for the cases when we need a signle check only. Amends 678e9f614bc5a05d2ff16cf916397998e7cdfca1. Change-Id: If2ab48ef910e97f241f5922d4108a271bc532f3a Reviewed-by: Thiago Macieira (cherry picked from commit 8784ea16a6bc66ac481d5cbf2dd1ece2d57a836b) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit b2bb9ef0f187a3c3d22c7f7714c038d07f87bb1b) Reviewed-by: Volker Hilsheimer --- src/corelib/global/qcompare.h | 7 ++++--- src/corelib/global/qcomparehelpers.h | 14 +++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/corelib/global/qcompare.h b/src/corelib/global/qcompare.h index 0966cdfe9e..cda5b4c595 100644 --- a/src/corelib/global/qcompare.h +++ b/src/corelib/global/qcompare.h @@ -641,14 +641,15 @@ auto qCompareThreeWay(const LeftType &lhs, const RightType &rhs); template - || QtOrderingPrivate::CompareThreeWayTester::hasCompareThreeWay, + std::disjunction_v< + QtOrderingPrivate::CompareThreeWayTester::HasCompareThreeWay, + QtOrderingPrivate::CompareThreeWayTester::HasCompareThreeWay>, bool> = true> auto qCompareThreeWay(const LT &lhs, const RT &rhs) noexcept(QtOrderingPrivate::CompareThreeWayTester::compareThreeWayNoexcept()) { using Qt::compareThreeWay; - if constexpr (QtOrderingPrivate::CompareThreeWayTester::hasCompareThreeWay) { + if constexpr (QtOrderingPrivate::CompareThreeWayTester::hasCompareThreeWay_v) { return compareThreeWay(lhs, rhs); } else { const auto retval = compareThreeWay(rhs, lhs); diff --git a/src/corelib/global/qcomparehelpers.h b/src/corelib/global/qcomparehelpers.h index dcaa886bcd..a44569aa72 100644 --- a/src/corelib/global/qcomparehelpers.h +++ b/src/corelib/global/qcomparehelpers.h @@ -873,12 +873,15 @@ using Qt::compareThreeWay; // Check if compareThreeWay is implemented for the (LT, RT) argument // pair. template -constexpr inline bool hasCompareThreeWay = false; +struct HasCompareThreeWay : std::false_type {}; template -constexpr inline bool hasCompareThreeWay< +struct HasCompareThreeWay< LT, RT, std::void_t(), std::declval()))> - > = true; + > : std::true_type {}; + +template +constexpr inline bool hasCompareThreeWay_v = HasCompareThreeWay::value; // Check if the operation is noexcept. We have two different overloads, // depending on the available compareThreeWay() implementation. @@ -886,12 +889,13 @@ constexpr inline bool hasCompareThreeWay< // context. template , bool> = true> + std::enable_if_t, bool> = true> constexpr bool compareThreeWayNoexcept() noexcept { return noexcept(compareThreeWay(std::declval(), std::declval())); } template && hasCompareThreeWay, + std::enable_if_t>, + HasCompareThreeWay>, bool> = true> constexpr bool compareThreeWayNoexcept() noexcept { return noexcept(compareThreeWay(std::declval(), std::declval())); }