Object-Oriented Programming

rValue References

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron


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

  • Left side of an assignment
  • E.g., int area = height * width, area is the lvalue
  • An lvalue is required as the target of an assignment


An expression that is not an lvalue

  • Right side of an assignment
  • E.g., int area = height * width, height * width is the rvalue
  • An rvalue cannot be the target of an assignment


  • foo() returns an lvalue
  • bar() returns an rvalue
  • i is an lvalue
  • A call to foo() is an lvalue
  • Can point to the result of a call to foo()
  • Cannot point to the result of a call to bar()

Copy vs. Swap

  • Copy field through copy constructor
  • Copy field through assignment
  • Swap field through 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 create a swap() method
  • Need to remember to use the swap() method
  • Not actually a move, but a swap where something is going away
  • Cannot use with temporary objects because they are not addressable

Temporary objects are not lvalues


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

Current IN Parameters

Current IN Parameters + r-value

swap() Implementation


  • Converts an lvalue into an rvalue
  • Moves the data from the right to the left
  • The argument object is left in a "valid but unspecified state"

std::move() Implementation

Using std::move()

Producer Consumer