C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Force compilers to warn about double moves

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Fri, 23 Jun 2023 09:48:58 +0200
If you do not want to allow double-move for certain code in the future, better use relocation (according to one of the proposals, whatever is accepted) there.   For those the object is not meant to be used afterwards and the compiler could (if it is detectable) abort with an error.   So it would be better fitting to your proposal to build on relocation.   -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Fr 23.06.2023 09:34 Betreff:Re: [std-proposals] Force compilers to warn about double moves An:std-proposals_at_[hidden]; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; On Thu, Jun 22, 2023 at 6:17 PM Hyman Rosen via Std-Proposals <std-proposals_at_[hidden]> wrote: > > It may also be reasonable for someone to do > > void F(C &&o) { >     A_MightTake(move(o)); >     B_MightTake(move(o)); >     ... > } > > to let a sequence of functions examine an object > and maybe take it, maybe not.  First one that > wants it, gets it, the rest see a moved-from > empty object. Okay then how about my proposed compiler warning/error only kicks in when then Rvalue reference is marked with postfix '&&'   ? So the following would fail to compile:    void F(C &&o&&)    {        A_MightTake( o );        B_MightTake( o );  // possible double-move    } And in order to alleviate this error, you would abandon the use of the postfix '&&' operator, and you would simply mark the function parameter as "&&o" instead of "&&o&&", and use 'std::move' as normal. By the way, when it comes to marking a function parameter with both prefix and postfix '&&', it would have no effect in a function declaration, i.e. the following two function signatures are equivalent:    void F(C &&o);    void F(C &&o&&); There would be a difference however in the function definition as follows:    void F(C &&o&&)    {        // In here, 'o' is always an R-value (no need for 'move' or 'forward')    } So if you decide to change a function parameter from "&&o" to "&&o&&", then none of the header files need to change. -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-06-23 07:49:00