public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/59926] Remove temporary move constructor before move assignment Date: Mon, 25 Mar 2024 12:43:35 +0000 [thread overview] Message-ID: <bug-59926-4-BRlJCtSLG6@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-59926-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59926 Jonathan Wakely <redi at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://gcc.gnu.org/bugzill | |a/show_bug.cgi?id=57176 Resolution|--- |INVALID Status|UNCONFIRMED |RESOLVED --- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Patrick Arnoux from comment #2) > Derived f(Derived d) { return (d) ; } > Derived g(Derived& d) { Derived e = d ; return (e) ; } > Derived h(Derived d) { Derived e = d ; return (e) ; } > > > Derived r ; > Derived u ; > u = f(r) ; // (A) Move Ctor to TmpObj, then Move Assign to u. > u = g(r) ; // (B) Move Assign to u (No move ctor) > u = h(r) ; // (C) Move Assign to u > Derived v = h(r) ; // (D) straight up 'rvo' > > I would have expected case A to behave like B and C and I would ask I don't think the standard allows that. The initial copy of 'r' has to happen in the caller's scope, not inside the function. And so it can't be constructed in the return slot, because the caller doesn't know that the body of 'f' returns its parameter, rather than returning some other object. If the functions are defined inline and your objects don't have silly side effects like printing messages to standard output for every member function (or other side effects with observable behaviour that can't be optimized away), then the compiler _will_ optimize it to more efficient code. > if case D could eliminate rvo and do a Move Ctor instead, would that > simplify the code generation. That's not possible, for the same reason as above. The call to h(r) has to make a copy in the caller's frame, and that obviously can't be a move because 'r' can't be modified by calling h(r). Then in the body of h you make another copy, which can't be changed to a move because the compiler isn't allowed to do that to arbitrary copies (except in a return statement). If you want it to be a move, write it as a move: Derived h(Derived d) { Derived e = std::move(d) ; return (e) ; } Of course that 'h' function is still unnecessarily inefficient, using 'f' with RVO does what you want: Derived w = f(r) ; 0x7ffd118b2cbf Derived copy 0x7ffd118b2cb7 Derived move 0x7ffd118b2cbf Derived dtor This copies 'r' to initialize the argument 'd' and then moves that directly into 'w'. The standard doesn't permit us to do anything different here, so I'm closing this.
prev parent reply other threads:[~2024-03-25 12:43 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2014-01-23 18:11 [Bug c++/59926] New: " arnoux123 at gmail dot com 2014-01-23 18:14 ` [Bug c++/59926] " redi at gcc dot gnu.org 2014-01-23 22:15 ` arnoux123 at gmail dot com 2024-03-17 4:49 ` pinskia at gcc dot gnu.org 2024-03-25 12:43 ` redi at gcc dot gnu.org [this message]
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-59926-4-BRlJCtSLG6@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).