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

Prav pr12og2 at programist.ru
Sun Jun 28 19:28:46 CEST 2015


> When a container calls detach(), it means it is no longer attached to the
> container it was copied from (if it was copied)
OK, thanks ... this clarify what detach means copying of container.

But I still can not get additional danger in using iterator-base loops:

Gunnar Roth - I woud suggest to not use QFOREACH, use c++11 range-based for loop.

Koehne Kai  - I'd be careful with such an advice, unless you understand the performance
                implications (because range-based loop will sometimes detach the Qt Container)

I can only imagine that Koehne Kai forgot what we are talking about ...

For things like:

  typedef int Item; //Select any type you like here
  typedef QVector<Item> Container; //Select any container you like here

  Container container;
  <<fill-container-code>>


Gunnar Roth is suggesting instead of:
  foreach(const auto &item, container) {
    <<loop-code>>
  }


use:
  for (Container::const_iterator iter=container.begin(); iter!=container.end(); ++iter) {
    const Item &item=*iter;

    <<loop-code>> // the same as in previous loop
  }

And how it is possible to detach something in the last example? I do not even see what to detach from what here?

So Koehne Kai's remark seems strange for me.




More information about the Interest mailing list