Palo Alto: Return Value Optimization Is Harder Than It Looks

Hosted by Association of C/C++ Users - San Francisco Bay Area

Public group

This is a past event

113 people went

Location image of event venue


Notice that this is a first Wednesday, but we are meeting in Palo Alto, not San Francisco! Also, this is a different A9 building from the usual discussion-group meetings. It's the same place we were in last August (, if that helps.

Join Arthur O'Dwyer on a deep dive into Return Value Optimization and related topics. Even C++03 allowed the compiler to "elide" copy construction in certain cases. 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 commonly received wisdom these days is that `return std::move(x)` is never necessary and often a pessimization.

...Or is it? We'll show some real-world examples where `return x` quietly copies a large object instead of moving it. We'll explain the little-known notion of a "converting constructor" and the precise way in which C++17 dropped the ball on these examples. Finally, Arthur has implemented a Clang compiler warning ( to detect and suggest fixes for problematic return statements; he'll walk through the relatively painless process of creating that diagnostic, and perhaps inspire you to contribute to Clang yourself!

Arthur O'Dwyer is the author of Mastering the C++17 STL ( and the principal organizer of the ACCU Bay Area meetup. He currently works for Akamai in San Francisco.