Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Copy elision and return value optimization

Reference for C++ copy elision: guaranteed elision of prvalues since C++17, RVO versus NRVO, when std::move on return hurts, and move-only return types.

Copy elision lets the compiler skip a copy or move and construct an object directly in its final location. Since C++17 one important case is guaranteed by the language: when you initialize an object from a prvalue (a temporary expression such as T() or a function call returning T by value), no temporary is created at all. The copy and move constructors need not even exist.

A second case, named return value optimization (NRVO) for returning a local variable by name, is still optional. Compilers do it routinely, but the standard does not require it, and the copy or move constructor must remain accessible. The practical advice is short: return locals by value, do not write std::move on them, and let the compiler do the work.

C++
Widget make() { return Widget(); }   // C++17: guaranteed, no copy, no move
Widget w = make();                   // w is built directly here

Try it

C++ Guaranteed elision with a non-movable type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ NRVO, std::move on return, and a conditional return

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

There is no syntax to request elision. It applies to these forms:

C++
T x = T(args);          // prvalue initializes x: guaranteed since C++17
T f() { return T(); }   // returning a prvalue: guaranteed
T f() { T t; return t; }// NRVO: permitted, not required
throw t;  /  catch (T t) // permitted in some cases
Case Status
Initializing from a prvalue of the same type (including a function's return value) Guaranteed since C++17; no copy/move constructor needed.
Returning a local variable by its name (NRVO) Permitted, optional. If not elided, the object is first treated as an rvalue (move).
Returning a function parameter Cannot be elided, but is implicitly moved.
Returning a member or a global A copy. Use std::move only deliberately.
return std::move(local); Prevents NRVO; the move constructor runs. Avoid.

Errors / undefined behavior: none. Elision is only visible through side effects of constructors and destructors (like the prints above). Code must not depend on the number of copies made, except in the guaranteed prvalue case.

Usage notes

  • C++17 changed the meaning. Before C++17 elision of temporaries was an optimization that still required an accessible copy or move constructor. Now a prvalue is not a temporary object that gets copied; it is an initializer for the destination.
  • Return local variables by value. Do not return const T by value (it blocks moves) and do not return references to locals. Writing return local; gives the compiler the best chance and falls back to a move.
  • std::move on return is usually wrong. GCC and Clang warn about it with -Wpessimizing-move and -Wredundant-move. The exception: returning a member of a local (return std::move(obj.member);) or a variable whose type differs from the return type may need it.
  • NRVO needs a single object. If different return statements return different locals, most compilers cannot elide. Restructure to one named result when you care.
  • Elision can hide side effects. If a copy constructor prints or counts, results differ from older standards. Do not put essential logic in copy constructors.
  • Non-movable return types work. std::mutex, std::atomic and std::lock_guard can be returned by prvalue from a factory function since C++17.
  • Compile flag. -fno-elide-constructors turns off the optional elisions (but not the guaranteed ones in C++17 mode) so you can see the language-level behavior. Not for production.
  • Unobserved allocations. The compiler may also elide heap allocations that have no observable effect; make effects observable if a demo depends on them.