[Interest] constexpr QString and QByteArray
Thiago Macieira
thiago.macieira at intel.com
Sun Sep 6 18:40:02 CEST 2026
[If you can't upgrade to C++20 yet, you can stop reading]
Starting with Qt 6.13, both QString and QByteArray can be created constexpr if
you are compiling your code with C++20 or higher. It's just as simple as:
using namespace Qt::StringLiterals;
constexpr QString varname = u"some string"_s;
constexpr QByteArray varothername = "Hello World";
That's it.
There are a couple accessor methods that are usable in constexpr contexts too,
such as at(), size(), isEmpty(), conversion to std::string_view /
u16string_view and, for QByteArray, the begin() & end() iteration (QString
does some reinterpret_cast'ing, which makes proper iteration not compile).
Both operator""_s and operator""_ba are constexpr too, as are the macros
QStringLiteral() and QByteArrayLiteral().
Enjoy!
--
Thiago Macieira - thiago.macieira (AT) intel.com
Principal Engineer - Intel DCG - Platform & Sys. Eng.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5176 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20260906/13887d27/attachment.bin>
More information about the Interest
mailing list