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

Olivier Goffart olivier at woboq.com
Sun Oct 28 19:49:08 CET 2018


On 10/27/18 5:27 PM, Sérgio Martins wrote:
>> Den lör 27 okt. 2018 kl 13:37 skrev Olivier Goffart <olivier at woboq.com>:
>>> Jokes aside, I think we still should let users use Q_FOREACH for implicitly
>>> shared containers.
> 
> But what's the percentage of Qt developers that understand the
> subtleties between Q_FOREACH and range-for ?
> Having a toolbox with two similar-but-not-quite constructs is bad for
> less seasoned users.
> I prefer explicitly assigning the container to a const local variable
> instead of relying in the magic behind macros.


I don't know... what's the percentage of Qt developers that understands when to 
use this qAsConst or const copy gymnastic?

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)

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.

-- 
Olivier

Woboq - Qt services and support - https://woboq.com - https://code.woboq.org



More information about the Development mailing list