[Qt-interest] Strange (as for me) QByteArray behavior

Thiago Macieira thiago at kde.org
Wed Dec 8 09:31:35 CET 2010


On Wednesday, 8 de December de 2010 09:07:52 Konrad Rosenbaum wrote:
> On Wednesday 08 December 2010, Ivan Kharin wrote:
> > > QByteArray tmp = username.toAscii();
> > 
> > const QByteArray &tmp = username.toAscii(); // avoid copy: const ref to
> > function result == //   keep automatic variable intact until end of
> > scope
> 
> I don't think this can work reliably. C++ is not a garbage collected
> language, the reference becomes invalid immediately after the semicolon.

That's not correct. The above is perfectly valid C++.

I've seen experimental compilers choke on it though (clang). But that's a 
compiler bug and it got fixed.

> Besides: it is unnecessary. QByteArray is implicitly shared - so very
> little data is actually copied.

That's also true. It's just a reference counting up an down.

With C++0x, we'll probably do a move constructor too.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      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-project.org/pipermail/qt-interest-old/attachments/20101208/99eaa69c/attachment.bin 


More information about the Qt-interest-old mailing list