[Qt5-feedback] C++0x: QString literals

Thiago Macieira thiago at kde.org
Wed Jul 13 15:56:34 CEST 2011


On Wednesday, 13 de July de 2011 15:35:32 João Abecasis wrote:
> Stefan Majewsky wrote:
> > C++0x seems to be adding support for user-defined literals. [1] Is
> > Nokia planning to add support for this to replace e.g. QLatin1String?
> > 
> > Code could then look like this:
> >> void f(const QString&);
> >> //...
> >> f(QLatin1String("foobar")); //C++ pre-0x
> >> f("foobar"q); //C++0x
> 
> [...]
> 
> > [1] http://en.wikipedia.org/wiki/C++0x#User-defined_literals
> 
> Since there's no compiler currently supporting user-defined literals we
> don't have plans to use that feature at the moment, although I've seen the
> idea entertained internally.
> 
> That said, we are adding support for having QString and QByteArray use
> static read-only data. This reduces the need for run-time initialization
> and memory allocation giving you essentially "free" read-only QString.

Actually, our solution is much better, even though you still have to write 
more. See:

https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/tools/qstring.h#line126

If you write:
	f(QStringLiteral("foobar"));

Then "foobar" is stored in static, read-only memory. There's no conversion 
cost or memory allocation cost at all.

The equivalent user-defined literal would be:
	f(u"foobar"q);

but it would involve at least one memory allocation. In any case, we cannot 
even experiment with the feature yet, because as João says, no compiler 
supports this yet.

PS: stop talking about "Nokia planning". The only thing that matters is the Qt 
community.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt.nokia.com/pipermail/qt5-feedback/attachments/20110713/def6ff66/attachment.bin 


More information about the Qt5-feedback mailing list