Named Variables and rvalues

Learn about the fundamentals of named variables and rvalues.

Concept of named variables and rvalues

So, when is the compiler allowed to move objects instead of copying? As a short answer, the compiler moves an object when the object can be categorized as an rvalue. The term rvalue might sound complicated, but in essence, it is just an object that is not tied to a named variable for either of the following reasons:

  • It's coming straight out of a function

  • We make a variable an rvalue by using std::move()

The following example demonstrates both of these scenarios:

Get hands-on with 1400+ tech skills courses.