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

Sérgio Martins sergio.martins at kdab.com
Mon Oct 29 17:05:44 CET 2018


On 2018-10-29 15:56, Thiago Macieira wrote:
> On Monday, 29 October 2018 04:43:09 PDT Olivier Goffart wrote:
>> > 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.
>> 
>> But using for(:) with a Qt container will cause a detach in the most 
>> common
>> case, so I'd say is even worse.
>> Which is why i'm saying using this reason is a bit ironic.
> 
> True, which is why we didn't deprecate, but did add qAsConst.
> 
>> > 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_)


That's not behaviour-compatible and will introduce bugs where users rely 
on iterating over a copy.




Regards,
-- 
Sérgio Martins | sergio.martins at kdab.com | Senior Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel: Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - The Qt, C++ and OpenGL Experts



More information about the Development mailing list