[Development] User-defined literals for QString (and QByteArray)

Andrei Golubev andrei.golubev at qt.io
Wed Mar 3 16:53:25 CET 2021


I've been now working for a while with literals that have to (eventually) be converted to QString one way or another. While in certain cases (e.g. myMsg == u"This is my message" or u"Hello, " + world) character literals are handled neatly by some underlying machinery, pure assignments just don't work well:

QString hello = u"Hello"; // oops, compilation error

There's an explicit ctor that accepts QChar*, but if you end up with it, QString would allocate a memory to store whatever QChar* points to. Same is true for QString::fromUtf16().

There's QStringView (and friends) which may aid you, but what if there's a list of strings that is populated? QList<QStringView> is not really an option if half of the strings are not literals. Then the next choice is QStringList in which case you need to do QStringView::toString() - well, bad luck, you end up allocating memory again.

There's of course QStringLiteral:
QString hello = QStringLiteral("Hello"); // yay, it works
... and, actually, QStringLiteral in Qt 6 is great, because it doesn't allocate the memory* to store that literal.
But writing "QStringLiteral" each time is a bit inconvenient: let's be honest, it's a lot of code to type. You may want to write it once or twice, but how about a fifty or a hundred times?

* unless you modify the resulting string, of course

Here's my proposal: let's introduce a user-defined literal [1] for QString (and I volunteer to do it).

How this would look for a user: instead of typing QStringLiteral("Hello") you would have something like u"Hello"_q. And the new code of course behaves exactly like the QStringLiteral()

Well, to be good citizens, we probably need to scope the user-defined literal (e.g. similarly to std::chrono_literals [2]). Given that, how you would really write your code is similar to:

using namespace <whatever>; // done only once in a .cpp file
u"Hello"_q; // "u" in front and suffix "_q" are written every time

This is the proposal in a nutshell. I'd like to have some feedback and then some suggestions regarding:

  *   Whether we want this for QByteArray as well (similarly to QString it allows "from raw data" construction, but then it's not really a string class, is it?)
  *   What suffix to use: in the example above I used "_q", but maybe "_qs" is better or something totally different
  *   How to call a namespace: I'd probably go with "QtLiterals" but (looking at [2]) maybe there should be a richer hierarchy? For example, "QtLiterals::StringLiterals", so that the suffix could be reused without conflicts, etc.
  *   Whether *View classes need it (in theory they should allow implicit construction, so probably not)

[1]: https://en.cppreference.com/w/cpp/language/user_literal
[2]: https://en.cppreference.com/w/cpp/symbol_index/chrono_literals

Best Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20210303/d8cf6a66/attachment.html>

More information about the Development mailing list