QStringView: add missing constexpr so we can use is_constant_evaluated

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 <marc.mutz@qt.io>
bb10
Thiago Macieira 2022-02-10 11:20:25 -08:00 committed by Marc Mutz
parent 98de89cc15
commit 4fd4082c3a
1 changed files with 1 additions and 1 deletions

View File

@ -149,7 +149,7 @@ private:
using if_compatible_container = typename std::enable_if<QtPrivate::IsContainerCompatibleWithQStringView<T>::value, bool>::type;
template <typename Char>
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())