Den sön 21 okt. 2018 kl 17:50 skrev Giuseppe D'Angelo <giuseppe.dang...@kdab.com>: > > Hello, > > Il 21/10/18 16:15, Elvis Stansvik ha scritto: > > I couldn't find a way to contact them. > > The best shot would be the std-discussion mailing list, I think. > > > In order to try out the unsafe usage you suggested in your other mail, > > and also another unsafe usage pointed out in an SO question > > (https://stackoverflow.com/questions/39051460/why-does-as-const-forbid-rvalue-arguments/39051612#39051612), > > I made the following test program. > > > > The output when running is: > > > > [estan@newton move-to-const-test]$ ./move-to-const-test > > without moveToConst: > > FooPrivate constr from vector > > Foo constr with arg 0x7fffdb627200 > > Foo begin 0x7fffdb627200 > > Foo end 0x7fffdb627200 > > Foo destr 0x7fffdb627200 > > FooPrivate destr > > > > with moveToConst: > > FooPrivate constr from vector > > Foo constr with arg 0x7fffdb627208 > > Foo move constr 0x7fffdb627210 > > Foo destr 0x7fffdb627208 > > Foo begin const 0x7fffdb627210 > > Foo end const 0x7fffdb627210 > > Foo destr 0x7fffdb627210 > > FooPrivate destr > > [estan@newton move-to-const-test]$ > > > > Which just shows it's working as intended. > > However the third test should be a without moveToConst, but storing in a > temporary, i.e. the current best practice. It should output exactly like > the first one, but of course call const begin/end. > > And note the extra move/destruciton in the second example.
Yep, well aware of the extra move/destruction. It's the price we pay for saving that extra line of code to define a const variable. > > > > The two unsafe usages are commented out, because they wouldn't compile > > (good!). > > > Whops, you're absolutely right. My bad. With your proposed implementation: > > > template <typename T> > > const T moveToConst(T &&t) > > { > > return std::move(t); > > } > > This won't compile when called on a non-const lvalue (the return type > will be a lvalue reference to const, which won't bind to a non-const > rvalue). I stand corrected. > > Which actually makes me think of yet another possibility of misuse, that > is, applying moveToConst to a function returning a const object. That > will compile, using a copy... > > > const QVector<Obj> getVector(); > > for (auto &obj : moveToConst(getVector()) ~~~ Yes, that would be useless, but at least not UB :) > > > Long story short, I think it's good if we stay away from this in Qt. > Clazy warns you if you "do it wrong", and being a Qt-specific problem, > we better not offer too many ways that could have unpleasant drawbacks. Yea, I can understand that. And since all it saves is a line of code, I guess not worth the trouble. Elvis > > > My 2 c, > -- > Giuseppe D'Angelo | giuseppe.dang...@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 > _______________________________________________ Development mailing list Development@qt-project.org http://lists.qt-project.org/mailman/listinfo/development