A video episode explains how return value optimization (RVO) and named return value optimization (NRVO) work in C++, and clarifies when std::move should and shouldn't be used when returning values from functions.

1m read timeFrom andreasfertig.com
Post cover image

Questions this post answers

When should I use std::move when returning a value from a C++ function?

Generally you should not use std::move on a local variable being returned, because it can suppress return value optimization or named return value optimization (NRVO), forcing a move instead of eliminating the copy entirely. The compiler already applies RVO/NRVO automatically in most cases, making an explicit std::move unnecessary and sometimes counterproductive. Developers sorting out move semantics pitfalls can find deeper C++ explainers curated on daily.dev.

137 Impressions