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

Thiago Macieira thiago.macieira at intel.com
Mon Jun 29 02:06:22 CEST 2015


On Monday 29 June 2015 00:40:29 Prav wrote:
> It is nice to have such time ... I answered you sharply because you gave
> sharp and short answer and you did not give any supporting arguments for
> your suggestion ... now you are giving them. Thanks!

You're the one asking for help. You must not upset the people trying to help 
you.

> >>  for (Container::const_iterator iter=container.begin();
> >>  iter!=container.end(); ++iter) {> 
> > And that could possibly detach. But of course only if container is sharing
> > data.
> This is the point ... if we use iterator-based loop there is nothing to
> detach from (comparing to foreach loop which makes container copy which
> attaches to shared data). That it why it is not clear why Koehne Kai warns
> about detaching in iterator based for-loops which you suggested as a
> replacement of foreach-loops!

Because you're wrong and the above can still detach.

> I can also add that ... for containers there is "const-versions" of begin()
> and end() which returns const_iterator ... so this const-version will be
> used. This means that there should not be no detachment if:
>   for (Container::const_iterator iter=container.begin();
> iter!=container.end(); ++iter) is used.

That can still detach. You want cbegin() and cend().

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




More information about the Interest mailing list