[Development] qMoveToConst helper for rvalue references to movable Qt containers?

Thiago Macieira thiago.macieira at intel.com
Sun Oct 28 20:17:25 CET 2018


On Sunday, 28 October 2018 11:49:08 PDT Olivier Goffart wrote:
> It is a bit ironic that one reason given to deprecate Q_FOREACH is that it
> may copy the container in some cases, while the alternative has the same
> problem in much more common cases. (It is my impression that implicitly
> shared container such as QList/QVector are by far much more used than the
> one that are not within a typical Qt code base)

There are two problems with Q_FOREACH:

1) it will copy containers. For Qt containers, that's rather cheap (two atomic 
refcount operations), but it's not free. And for Standard Library containers, 
that is likely very expensive.

2) it's implemented by way of a for loop inside a for loop, which is known to 
throw optimisers out, generating slightly worse code

Our rule already was: Don't use foreach in Qt code. (it was fine in examples)

Using iterators and now using the ranged for may need more typing, but 
produces optimal code.

> What could be done is to only deprecate partial specialization of
> qMakeForeachContainer for QVarLenghtArray and the standard containers.
> Or for containers that do not have a 'detach' function.
> That way, users would get a warning for the problematic containers, but
> would continue to work just fine with with the containers most Qt developer
> use.

I disagree. The optimisation problem is not solved.

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






More information about the Development mailing list