[Interest] QStringList, takeFirst and order of execution

Thiago Macieira thiago.macieira at intel.com
Mon Nov 11 13:42:36 CET 2013


On segunda-feira, 11 de novembro de 2013 13:37:20, Samuel Gaist wrote:
> Hi,
> 
> I have been experiencing a strange behavior with QStringList and the order
> of execution of takeFirst.

> qDebug() << lineData.takeFirst() << lineData.takeFirst();

The problem is not takeFirst.

> behaves differently on two computers.

Correct. You have undefined behaviour.

Both calls to takeFirst() are in the same "sequence-point". That means the 
compiler is allowed to execute in any order.

It would be the same if you had written:

	f(lineData.takeFirst(), lineData.takeFirst());

> qDebug() << lineData.takeFirst();
> qDebug() << lineData.takeFirst();
> 
> The order is correct on both of them.
> 
> What could explain the difference ?

In the second case, there are two sequence points (they are separated by a ;). 

In C++11-speak, the first line of the second example is guaranteed to "happens-
before" the second line. In the first example, there's no "happens-before", 
therefore any one can happen before the other, or after.
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131111/d2d0dd16/attachment.sig>


More information about the Interest mailing list