[Interest] QStringList, takeFirst and order of execution

2013-11-11 Thread Samuel Gaist
Hi,

I have been experiencing a strange behavior with QStringList and the order of 
execution of takeFirst.

The following code:

QString line = file.readLine();  // line contains 46 71\n
QStringList lineData = line.split(); // lineData contains (46, 71\n)
qDebug()  lineData.takeFirst()  lineData.takeFirst();

behaves differently on two computers.

The first one is a MacOS X 10.8.5 with Xcode 4.6.3 installed and using the 
5.2.0 alpha package with gcc
The second one is a MacOS X 10.8.5 with Xcode 5.0.1 installed and a developer 
build (no special configuration) of the latest stable branches from git with 
clang

The first computer outputs 71\n 46 and the second one 46 71\n

The same happens if I build e.g. a QSize using:

QSize mySize = QSize(lineData.takeFirst().toInt(), 
lineData.takeFirst().toInt());

on the other hand, if I call 

qDebug()  lineData.takeFirst();
qDebug()  lineData.takeFirst();

The order is correct on both of them. 

What could explain the difference ?
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] QStringList, takeFirst and order of execution

2013-11-11 Thread Thiago Macieira
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


signature.asc
Description: This is a digitally signed message part.
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] QStringList, takeFirst and order of execution

2013-11-11 Thread Giuseppe D'Angelo

Il 11/11/2013 13:37, Samuel Gaist ha scritto:

Hi,

I have been experiencing a strange behavior with QStringList and the order of 
execution of takeFirst.

The following code:

QString line = file.readLine();  // line contains 46 71\n
QStringList lineData = line.split(); // lineData contains (46, 71\n)
qDebug()  lineData.takeFirst()  lineData.takeFirst();


I've already explained this in the past but I can't find the relevant 
email. This last line is parsed as



operator( operator( qDebug(), lineData.takeFirst() ) , lineData.takeFirst() 
)


(operator is left associative).

Take the outer operator. C++ does not define the order of evaluating 
its parameters. That means that either lineData.takeFirst() can be 
called before evaluating the inner operator, or vice-versa. Depending 
on what the compiler chooses to do, you'll have a different result.


HTH,
--
Giuseppe D'Angelo | giuseppe.dang...@kdab.com | Software Engineer
KDAB (UK) Ltd., a KDAB Group company
Tel. UK +44-1738-450410, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions



smime.p7s
Description: Firma crittografica S/MIME
___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] QStringList, takeFirst and order of execution

2013-11-11 Thread Samuel Gaist

On 11 nov. 2013, at 13:42, Thiago Macieira thiago.macie...@intel.com wrote:

 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.

I see, thanks for the explanation !

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest


Re: [Interest] QStringList, takeFirst and order of execution

2013-11-11 Thread Samuel Gaist

On 11 nov. 2013, at 13:50, Giuseppe D'Angelo giuseppe.dang...@kdab.com wrote:

 Il 11/11/2013 13:37, Samuel Gaist ha scritto:
 Hi,
 
 I have been experiencing a strange behavior with QStringList and the order 
 of execution of takeFirst.
 
 The following code:
 
 QString line = file.readLine();  // line contains 46 71\n
 QStringList lineData = line.split(); // lineData contains (46, 71\n)
 qDebug()  lineData.takeFirst()  lineData.takeFirst();
 
 I've already explained this in the past but I can't find the relevant email. 
 This last line is parsed as
 
 operator( operator( qDebug(), lineData.takeFirst() ) , 
 lineData.takeFirst() )
 
 (operator is left associative).
 
 Take the outer operator. C++ does not define the order of evaluating its 
 parameters. That means that either lineData.takeFirst() can be called before 
 evaluating the inner operator, or vice-versa. Depending on what the 
 compiler chooses to do, you'll have a different result.
 

Ok, with that and the explanation of Thiago, it's crystal clear. Thanks !

___
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest