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 <a.samirh78@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Thiago Macieira 2023-05-20 08:37:03 -07:00
parent 631901f6d1
commit 95e6fac0a5
4 changed files with 28 additions and 14 deletions

View File

@ -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 <typename Enum>
constexpr std::underlying_type_t<Enum> qToUnderlying(Enum e) noexcept

View File

@ -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 <typename Char>
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<Char>::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:
//

View File

@ -105,8 +105,8 @@ private:
template <typename Char>
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<Char>::length(str);
#endif
return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));

View File

@ -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 <typename StringBuilder>