[Development] RFC: Proposal for a semi-radical change in Qt APIs taking strings
Marc Mutz
marc.mutz at kdab.com
Tue Oct 20 12:55:22 CEST 2015
On Tuesday 20 October 2015 09:13:18 Smith Martin wrote:
> I see. But then, if I have QStringView, doesn't that eliminate most of the
> reasons for needing the other string containing classes? If I want the
> efficiency of QStringView, won't QString underneath always be the right
> choice?
No. Two examples:
QStringLiteral - currently, it returns a full QString instance, with
associated need to call the out-of-line QString dtor. Q6String will solve many
of the issues, as Thiago has already said. But with QStringView, we could just
pass u"foo" (a const char16_t[4]) _now_, and be done with it. The compiler
and linker will share those string literals for us and even perform common
suffix optimisations ("bar" and "foobar" overlap in the executable - that's
allowed for string literals (and empty base classes) but not any other
objects). No QString involved.
If you build a local temporary string, it makes sense to use
QVarLengthArray<QChar> to do so. Again no QString involved.
> In Thiago's example, if I have a QStringView API on my class, would I ever
> want to switch the QString to a QByteArray?
Yes. See, say, QUrl, which needs to juggle both the encoded and the decoded
form of a URL.
Also, if you have an XML file, in most cases it will not be encoded in UTF-16
on disk, so what you're working off of is a QByteArray (yes, I realize that
QXmlStreamReader is working on QChar* (QStringRef, QString,
QXmlStreamStringRef, ...), so take emails instead.
> Does using QStringView with QString work as well or better than WByteArray
> et al?
QByteArray is to QString what QLatin1String is (a bit) to QStringView, but
we've been discussing (and agreeing on) the need for a QByteArrayView, too,
because not all QByteArrays contain latin-1 strings.
But those who wish to experiment with a string view in Qt can use
QLatin1String more. Now.
Thanks,
Marc
--
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - The Qt Experts
More information about the Development
mailing list