[Interest] Q_FOREACH creating copy , was (no subject)

Koehne Kai Kai.Koehne at theqtcompany.com
Fri Jun 26 17:28:20 CEST 2015



> -----Original Message-----
> From: interest-bounces+kai.koehne=theqtcompany.com at qt-project.org
> [mailto:interest-bounces+kai.koehne=theqtcompany.com at qt-project.org]
> On Behalf Of Gunnar Roth
> Sent: Friday, June 26, 2015 5:15 PM
> To: Prav
> Cc: interest at qt-project.org
> Subject: Re: [Interest] Q_FOREACH creating copy , was (no subject)
> 
> Hi Prav,
>  you think adding a subject to a mail is overrated? ;-)
> 
> I woud suggest to not use QFOREACH, use c++11 range-based for loop.

I'd be careful with such an advice, unless you understand the performance
implications (because range-based loop will sometimes detach the Qt
Container, which is potentially a lot more expensive than the shallow copy
foreach does). [1]

I think the advice should be to use foreach with Qt containers, range based
for for stl one's. I uploaded https://codereview.qt-project.org/#/c/115358/1
to make this part of the documentation.

> Betreff: [Interest] (no subject)
> Why it is done so that foreach statement is doing copying of container?
> 
> I can not imagine good cases of using current container-copy feature of
> foreach ... to be able to modify container while looping through its hiddenly
> copied duplicate ... well ... this is something ... let's say special ;)

There are multiple answers to this on stack overflow (see e.g. http://stackoverflow.com/questions/23612885/why-does-qt-foreach-create-a-copy-of-the-container).

Whatever the original reason was: The possibility to change the original 
container while iterating over it with foreach might be a bit 'special', but is not 
that uncommon. I don't think we can change therefore the behavior anymore.

Regards

Kai 



More information about the Interest mailing list