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