From 4fd4082c3ab682f14911e6308ec5ccb400de34f9 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 10 Feb 2022 11:20:25 -0800 Subject: [PATCH] QStringView: add missing constexpr so we can use is_constant_evaluated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GCC 12 -std=c++20 says: qstringview.h:155:39: error: ‘std::is_constant_evaluated’ always evaluates to false in a non-‘constexpr’ function [-Werror=tautological-compare] With this, it's possible to declare: constexpr QStringView sv = u"Hello, World!"; QStringView f() { return sv; } And GCC will generate: movl $13, %eax leaq .LC0(%rip), %rdx ret Writing simply QStringView f() { return u"Hello, World!"; } Causes GCC to emit a comparison loop (the std::char_traits::length function), but at least there's no function call in either C++17 or 20. Clang 13 is able to reduce the loop to a constant and produces the same code as the constexpr variable in either mode. Pick-to: 5.15 6.2 6.3 Change-Id: I74249c52dc02478ba93cfffd16d282fa35a5b883 Reviewed-by: Marc Mutz --- src/corelib/text/qstringview.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 5d05dd6edd..f229df9c21 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -149,7 +149,7 @@ private: using if_compatible_container = typename std::enable_if::value, bool>::type; template - static qsizetype lengthHelperPointer(const Char *str) noexcept + static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { #if defined(__cpp_lib_is_constant_evaluated) if (std::is_constant_evaluated())