Object-Oriented Programming

rvalue References

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

lvalue and rvalues

  • lvalue - e.g., area,

    An expression that refers to a memory location and therefore we can get the address of via the & operator

  • rvalue - e.g., height * width

    An expression that is not an lvalue


Copy vs. Swap

Move Semantics

  • Move data instead of copying
  • Can efficiently pass data from one part of the program to another, e.g., XMLDocument, without using pointers
  • Previously implemented via swap() type operations

Move Semantics Usage

Implementation Problems

  • Need to declare swap()
  • Need to remember to use swap()
  • Not really a move, but a swap where something is going away
  • Cannot use with temporary objects, because they are not addressable

Temporary Objects

  • Temporary objects are not lvalues (cannot take the address of them)


  • A& - lvalue reference
  • A&& - rvalue reference

swap() Implementation

std::move() Implementation

Using std::move()

Perfect Forwarding Solution