[Interest] QStringList, takeFirst and order of execution

Samuel Gaist samuel.gaist at edeltech.ch
Mon Nov 11 16:35:17 CET 2013


On 11 nov. 2013, at 13:50, Giuseppe D'Angelo <giuseppe.dangelo at 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 !




More information about the Interest mailing list