C++ Copy Elision and Return Value Optimization

  • RVO = Return Value Optimization
  • NRVO = Named Return Value Optimization
  • Copy Constructor (ctor)
  • Copy Assignment Operator
  • Move Constructor (ctor): C::C(C&& other);
  • Move Assignment Operator: C& C::operator=(C&& other);
  • Move Semantics
  • Temporary
  • Lvalue: object that occupies some identifiable location in memory (i.e. has an address)
  • Rvalue: temporary register (i.e. has NOT address)
  • Rvalue References: std::string&& rrstr;
  • Lvalue references: std::string& ref;
  • Reference Variables

Understanding lvalues and rvalues in C and C++

Six User-Declared Special Member Functions

  • Default constructor
  • Copy constructor (ctor)
  • Copy assignment operator
  • Move Constructor (ctor)
  • Move Assignment Operator
  • Destructor

C++ declares its remaining special member functions implicitly, if there are no user-declared functions.

Vokabular

  • pilfer = stehlen, entwenden, klauen, stibitzen
  • elision = Weglassung, Auslassung

Hitherto, copying has been the only means for transferring a state from one object to another.
And yet, in many real-world scenarios, you don’t copy objects but move them.
Notwithstanding the conceptual difference between copying and moving, there’s a practical difference too: Move operations tend to be faster than copying because they transfer an existing resource to a new destination, whereas copying requires the creation of a new resource from scratch.

Bislang war das Kopieren das einzige Mittel, um einen Zustand von einem Objekt auf ein anderes zu übertragen.
Doch in vielen realen Szenarien kopiert man Objekte nicht, sondern verschiebt sie.
Ungeachtet des konzeptionellen Unterschieds zwischen Kopieren und Verschieben gibt es auch einen praktischen Unterschied: Verschiebeoperationen sind tendenziell schneller als Kopieren, weil sie eine bestehende Ressource an ein neues Ziel übertragen, während beim Kopieren eine neue Ressource von Grund auf neu erstellt werden muss.

Übersetzt mit www.DeepL.com/Translator (kostenlose Version)

Is there any gorgeous way for C++ to return an object without copying it in memory?
Returning a c++ std::vector without a copy?
What are copy elision and return value optimization?
Move or Named Return Value Optimization (NRVO)?
What Is RVO And NRVO In C++? (YouTube Video)

C++11 Tutorial: Introducing the Move Constructor and the Move Assignment Operator

cppreference.com

Copy elision
Move constructors

Leave a Reply

Your email address will not be published. Required fields are marked *