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

Olivier Goffart olivier at woboq.com
Mon Oct 29 17:05:37 CET 2018


On 10/29/18 4:56 PM, Thiago Macieira wrote:
>>> 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
>>
>> I would consider that the missed optimization is quite small, if not
>> negligible. And it can be solved in C++17:
>> https://codereview.qt-project.org/243984/
> 
> I'd solve this the other way around, by making the macro:
> 
>      if (const auto &_container_ = (container); true) \
>          for (variable : _container_)
> 
> This creates a scope-only const reference to the original container and then
> uses the ranged for on it.

That does not work, because foreach need to support a variable which is already 
declare: "QString str; foreach (str, list)"  (see the documentation
http://doc.qt.io/qt-5/containers.html#foreach ), and this construct is not 
allowed in a ranged-based for.

Othewise, we could have that in C++11:

     for (variable : qMakeForeachContainer(container))

> We should be able to depend on this for Qt 6.4 or something.

I tought Qt6 would probably require C++17. But even if one does not, it's 
completely optional.




More information about the Development mailing list