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

Alejandro Exojo suy at badopi.org
Mon Jun 29 00:13:29 CEST 2015


El Sunday 28 June 2015, Prav escribió:
> 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.

Say that you received the container as a parameter to your function by 
constant reference. Then begin() will return the constant iterator, and will 
not call detach(), nor make an actual copy of the data stored in the 
container.

If you received instead the container by value, calling begin() will tell the 
container that you need to modify the container, because the iterator gives 
you access to the data. The container will need to copy the data this time.

The const version will only be called if the container is constant.

-- 
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net



More information about the Interest mailing list