[MUC++] Arthur O'Dwyer - "The Complete Guide to return x;" (Virtual)
Channel: MUCplusplus
Date: 08/05/2020

Join Arthur O'Dwyer on a deep dive into Return Value Optimization, implicit move, and related topics. C++ has always been able to "elide" copy construction in certain cases. We'll start with an explanation of the "return slot" and show why 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. However, even in C++17, `return x` would sometimes quietly copy a large object instead of moving it. C++2a adopted Arthur's P1155 "More Implicit Moves," which means that many of these cases have been fixed. We'll see a few cases that remain troublesome. Furthermore, C++2a 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++2a can be used in the "perfect returning idiom." ...Or can it? We'll review various late-breaking issues surrounding implicit move and suggest resolutions for them.