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

Prav pr12og2 at programist.ru
Sat Jun 27 15:33:31 CEST 2015


> 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.
Thanks!  By  could  you  explain  more  or get link to this detachment issue?

> 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.
I  agree  that  one  can  image  some  code  which uses this foreach's
feature.
But I was asking another thing:
Although current implementation of iterator-loop have the right to exist but in this case I would expect it to co-exist with another iterator-loop which works in more straightforward for developers way (with just const-ref to container).
So the question is why we do not have const-ref to container loop.
What about adding let's say "forevery" which will use const-ref to container. It seems easy to do.
At least it would be more clear case as we would have both ways and do not make choice for Qt-user.


>> -----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 




-- 
С уважением,
 Prav                          mailto:pr12og2 at programist.ru




More information about the Interest mailing list