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

Koehne Kai Kai.Koehne at theqtcompany.com
Tue Jun 30 14:04:01 CEST 2015



> -----Original Message-----
> From: interest-bounces+kai.koehne=theqtcompany.com at qt-project.org
> [...]
> >> for (Container::const_iterator iter=container.cbegin();
> >> iter!=container.cend(); ++iter) {  //...
> >> }
> >>
> >> But why this could be more insecure to container copy (as result of
> detaching) as Koehne Kai warns:
>
> >>    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)
> >>
> >> is still unclear for me.
> 
> But I still see nothing bad in using iterator-bases loop for Qt-containers.

There's nothing bad with using STL iterators with Qt containers, and I didn't intend to say anything like this. In fact, if you really want to have the best performance and don't mind the verbosity,

  for (auto iter = myQtContainer.cbegin(); iter != myQtContainer.cend(); ++iter)

is guaranteed to have the least overhead.

My comment was just about range-based for, that is

  for (auto value, myQtContainer) {
  }

And that this might be more expensive than expected, because it calls begin(), which might detach. That's all.

Kai 



More information about the Interest mailing list