From 95e6fac0a5a1eee3aa23e4da0a93c6c25e32fb98 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sat, 20 May 2023 08:37:03 -0700 Subject: [PATCH] Short-live qIsConstantEvaluated() This is not q20::is_constant_evaluated() because it does not replace that for all compilers. Instead, it's our own version of it that may return false even in constant contexts. However, for the majority of our users, it will work even in C++17 mode. Updated QStringView and QAnyStringView to use it, which are the only two places in all of Qt that used std::is_constant_evaluated(). Change-Id: Ieab617d69f3b4b54ab30fffd175c50c517589226 Reviewed-by: Ahmad Samir Reviewed-by: Thiago Macieira --- src/corelib/global/qttypetraits.h | 17 +++++++++++++++++ src/corelib/text/qanystringview.h | 15 ++++----------- src/corelib/text/qstringview.h | 4 ++-- .../text/qanystringview/tst_qanystringview.cpp | 6 +++++- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/corelib/global/qttypetraits.h b/src/corelib/global/qttypetraits.h index 49f2728e9f..9129f536aa 100644 --- a/src/corelib/global/qttypetraits.h +++ b/src/corelib/global/qttypetraits.h @@ -17,6 +17,23 @@ QT_BEGIN_NAMESPACE +// like std::is_constant_evaluated +#define QT_SUPPORTS_IS_CONSTANT_EVALUATED +#ifdef __cpp_lib_is_constant_evaluated +constexpr bool qIsConstantEvaluated() noexcept +{ + return std::is_constant_evaluated(); +} +#elif __has_builtin(__builtin_is_constant_evaluated) || \ + (defined(Q_CC_MSVC_ONLY) /* >= 1925, but we require 1927 in qglobal.h */) +constexpr bool qIsConstantEvaluated() noexcept +{ + return __builtin_is_constant_evaluated(); +} +#else +# undef QT_SUPPORTS_IS_CONSTANT_EVALUATED +#endif + // like std::to_underlying template constexpr std::underlying_type_t qToUnderlying(Enum e) noexcept diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index c2a4b9ad09..ac5cb54c78 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -101,12 +101,12 @@ private: static constexpr bool isAsciiOnlyCharsAtCompileTime(Char *str, qsizetype sz) noexcept { // do not perform check if not at compile time -#if !defined(__cpp_lib_is_constant_evaluated) +#if !defined(QT_SUPPORTS_IS_CONSTANT_EVALUATED) Q_UNUSED(str); Q_UNUSED(sz); return false; #else - if (!std::is_constant_evaluated()) + if (!qIsConstantEvaluated()) return false; if constexpr (sizeof(Char) != sizeof(char)) { Q_UNUSED(str); @@ -137,8 +137,8 @@ private: template static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { -#ifdef __cpp_lib_is_constant_evaluated - if (std::is_constant_evaluated()) +#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED + if (qIsConstantEvaluated()) return qsizetype(std::char_traits::length(str)); #endif if constexpr (sizeof(Char) == sizeof(char16_t)) @@ -289,13 +289,6 @@ public: [[nodiscard]] Q_CORE_EXPORT static int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; [[nodiscard]] Q_CORE_EXPORT static bool equal(QAnyStringView lhs, QAnyStringView rhs) noexcept; - static constexpr inline bool detects_US_ASCII_at_compile_time = -#ifdef __cpp_lib_is_constant_evaluated - true -#else - false -#endif - ; // // STL compatibility API: // diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 24a860c1a6..e4c0c4dd8d 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -105,8 +105,8 @@ private: template static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { -#if defined(__cpp_lib_is_constant_evaluated) - if (std::is_constant_evaluated()) +#if defined(QT_SUPPORTS_IS_CONSTANT_EVALUATED) + if (qIsConstantEvaluated()) return std::char_traits::length(str); #endif return QtPrivate::qustrlen(reinterpret_cast(str)); diff --git a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp index d5a62887e1..fa55bf5db2 100644 --- a/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp +++ b/tests/auto/corelib/text/qanystringview/tst_qanystringview.cpp @@ -449,7 +449,8 @@ void tst_QAnyStringView::basics() const void tst_QAnyStringView::asciiLiteralIsLatin1() const { - if constexpr (QAnyStringView::detects_US_ASCII_at_compile_time) { +#ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED + if constexpr (true) { constexpr bool asciiCstringIsLatin1 = QAnyStringView("Hello, World").isLatin1(); QVERIFY(asciiCstringIsLatin1); constexpr bool asciiUtf8stringIsLatin1 = QAnyStringView(u8"Hello, World").isLatin1(); @@ -466,6 +467,9 @@ void tst_QAnyStringView::asciiLiteralIsLatin1() const !QAnyStringView::fromArray(u8"Tørrfisk").isLatin1(); QVERIFY(utf8StringArrayIsNotLatin1); } +#else + QSKIP("Compile-detection of US-ASCII strings not possible with this compiler"); +#endif } template