C++Now 2021 has ended
Back To Schedule
Tuesday, May 4 • 1:30pm - 3:00pm
The Complete Guide to `return x;`

Log in to save this to your schedule, view media, leave feedback and see who's attending!

Join Arthur O'Dwyer on a deep dive into Return Value Optimization, implicit move, and related topics. We'll start with an explanation of the calling convention on a typical machine, showing where the mysterious "return slot" lives, and why it makes sense that C++03 allowed copy elision in certain special cases (but not others). Then we'll show how C++11 (and a subsequent defect report) upped the game by quietly turning the remaining copies into moves, so that the received wisdom these days is that `return std::move(x)` is never necessary and often a pessimization (because it disables RVO). However, even in C++17, `return x` would sometimes quietly copy a large object instead of moving it. C++20 adopted Arthur's P1155 "More Implicit Moves," which means that many of these cases have been fixed. Arthur will show one or two cases that remain troublesome.
Furthermore, C++20 adopted David Stone's P0527 "Implicitly move from rvalue references." This paper is a core-language game-changer: it permits `return x` to move out of the target of `x` when `x` is an rvalue reference. We'll show how this surprising new feature of C++20 can be used in the "perfect returning idiom." ...Or can it? We'll review various late-breaking issues surrounding implicit move and the solutions proposed in Arthur's P2266 "Simpler Implicit Move."

avatar for Arthur O'Dwyer

Arthur O'Dwyer

C++ Trainer
Arthur O'Dwyer is the author of "Mastering the C++17 STL" (Packt 2017) and of professional training courses such as "Intro to C++," "Classic STL: Algorithms, Containers, Iterators," and "The STL From Scratch." (Ask me about training your new hires!) Arthur is occasionally active on... Read More →

Tuesday May 4, 2021 1:30pm - 3:00pm MDT