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

Philippe philwave at gmail.com
Wed Oct 31 11:17:43 CET 2018


Good post, but:

>> * if you have a container and you want to do non-mutating iteration, use
>> for (const auto &c = container; const auto &element : c)

This is enough:

for (auto &element : std::as_const(container))

and for the rvalue reference case:

for (const auto &c = container; auto &element : c)

Philippe

On Wed, 31 Oct 2018 10:47:56 +0100
Giuseppe D'Angelo via Development <development at qt-project.org> wrote:

> Il 29/10/18 12:43, Olivier Goffart ha scritto:
> > Deprecating means you will force user to port all their codebase away from it,
> > which is a huge work. If the rationale is just that they will save a couple of
> > atomic operations, i do not think it is worth it.
> >
> > Deprecating it only for non-shared container seems more logical, since we then
> > warn only when there is actually a problem.
> >
> > And for the Qt shared container case, using foreach is less typing, but also
> > less error prone. (do i have to use qAsConst? or make a copy? or even return a
> > const object which even lead to more problems)
> 
> 
> Not really, my main argument is teachability. I want to stop teaching foreach to Qt users (which are also C++ users; why do we keep forgetting that there's a C++ world out there that doesn't use Qt?), and tell them that
> 
> * foreach is supposed to be used only on Qt containers, not STL / Boost ones
> * actually, only on _certain_ Qt containers,
> * but if you use it on a "wrong" container it still works, no warnings or anything, just a tremendous price hit,
> * why? because it's unconditionally doing a copy, which is cheap only for the certain containers in Qt,
> * and because of this copy it can't do mutating iteration, so you need to learn ranged-based for *anyhow*,
> * but thanks to this copy it is perfectly safe to modify the original container, because it's copied anyhow (and you can rely on it) (and documentation was encouraging this) (and Qt's OWN SOURCE CODE had places that relied on that) ---
> 
> Well, no. :(
> 
> 
> I want to teach users:
> 
> * if you have a container and you want to do mutating iteration, use
> 
> for (auto &element : container)
> 
> 
> * if you have a container and you want to do non-mutating iteration, use
> 
> for (const auto &c = container; const auto &element : c)
> 
> 
> And that's it; this is a pure C++ solution that works for any container (Qt, STL, Boost, ...), does never take any copy (you're iterating on the original), it is const-correct (can't accidentally modify the container through the element in a non-mutating iteration, can't accidentally modify a const container), never detaches a Qt container unless you need to anyhow (mutating iteration), and in general it is not safe to modify the container's structure from within the loop's body so don't do it.
> 
> My 2 c,
> 
> -- Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
> KDAB (France) S.A.S., a KDAB Group company
> Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
> KDAB - The Qt, C++ and OpenGL Experts
> 





More information about the Development mailing list