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

Elvis Stansvik elvstone at gmail.com
Sat Oct 27 17:46:00 CEST 2018


Den lör 27 okt. 2018 kl 17:33 skrev Sérgio Martins <iamsergio at gmail.com>:
>
> On Sat, Oct 20, 2018 at 1:44 PM Elvis Stansvik <elvstone at gmail.com> wrote:
> >
> > Hi all (first post),
>
> Welcome :)
>
> > In Qt 5.7+ there's qAsConst, an std::as_const implementation for those
> > who are not on C++17 yet, which is convenient for iterating over Qt
> > containers using range-based for loops without causing a detach.
> >
> > For good reasons there's no version of qAsConst that takes an rvalue
> > reference, so you can't do e.g. for (auto foo :
> > qAsConst(returnsQtContainer())  { ... }. Instead you must do const
> > auto stuff = returnsQtContainer(); for (auto foo : stuff) { ... }.
>
> Should we instead just encourage people to make returnsQtContainer()
> return a const container ?
> Not sure what's the reason we don't do it in Qt. It's frowned upon for
> regular value types, but our containers are special.

It was the very last comment on that blog post, to which Marc
answered: "Making returned containers const inhibits move semantics,
because const rvalues do not bind to the mutable rvalue references
that move constructors and move assignment operators use."

Elvis

>
> Regards,
> Sergio



More information about the Development mailing list