[Development] HEADS-UP: QStringLiteral

Bogdan Vatra bogdan.vatra at kdab.com
Tue Aug 20 17:56:06 CEST 2019


  Isn't silly to have so many wrappers around a such a simple thing as 
strings? All the major frameworks out there (i.e. Java, C#) they have a single 
String which does all the magic.

  In Qt we have: QString, lost of QString::fromXXX, QLatin1String, 
QStringLiteral, QStringView, QByteArray (which si a castrated version of 
QString for some kind of utf-8 strings) and of course a few QByteArray::from. 
You might think using QString{u"myString"} will save you from all these 
madness and to save a few CPU ticks, well, you have tot think again, 
QString{u"myString"} isn't working!
  Is it that hard to have *A* single String for UTF-16 and *A* single one for 
UTF-8? Ot at least A single String class and *A* single sting wrapper for each 


În ziua de marți, 20 august 2019, la 13:04:12 EEST, Mutz, Marc via Development 
a scris:
> Hi,
> In light of a recent attempt to re-introduce QStringLiteral, relacing
> QLatin1String, I'd like to remind everyone that QStringLiteral has a lot
> of drawbacks and should not be used unless it saves a memory allocation.
> Drawbacks include:
> - doubled storage requirements (2 bytes/char) over a L1/US-ASCII string
> - unsharability with C strings (each QStringLiteral contains a new
> lambda, lambdas have unique types, static data in token-for-token
> identical lambdas is duplicated, merging these violates the standard)
> - introduction of QString dtor calls (cannot be optimized away)
> Anti-patterns include: Using QStringLiteral in
> - comparisons (includes relational operators, indexOf, contains)
> - composition (QStringBuilder, append, prepend, arg() (now partly
> available on QStringView and QLatin1String, too)
> - lookup and, I believe, even insertion, in JSON and soon CBOR classes
> - debug statements (we prefer debug code to be compact, not fast)
> - arguments to any other function overloaded QStringView or
> QLatin1String
> Alternatives to QStringLiteral:
> - u"..." (→ QStringView)
> - QLatin1String
> Please take note of the following recent developments:
> - arg(string, ...., string) ("multiArg") is available now on QL1S and
> QStringView
>    - number formatting isn't, yet
> - You can use u"..." in Qt sources unconditionally
>    - converts implicitly to QStringView
> - To convert QString → QStringView, prefer qToStringViewIgnoringNull()
> when you don't need to distinguish between the null and the empty state.
> It saves a branch.
> Please help spreading this knowledge in review and your own work.
> Thanks,
> Marc
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> https://lists.qt-project.org/listinfo/development

More information about the Development mailing list