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

Thiago Macieira thiago.macieira at intel.com
Mon Jun 29 17:35:29 CEST 2015

On Monday 29 June 2015 16:48:10 Prav wrote:
> > Because you're wrong and the above can still detach.
> > That can still detach. You want cbegin() and cend().
> Good point ... I agree ... so here is the final "template" which is
> replacement for foreach 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.

Because it will go into UB if someone does modify the container.

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Interest mailing list