Understanding C++ Value Categories

This article aims to explain and clarify the different categories of values in C++. Historically, only lvalues and rvalues were used to classify expressions, but with the evolution of the language especially since C++11, new categories have been introduced to capture more precise semantics.

Value categories are one of the most fundamental yet misunderstood concepts in modern C++. Understanding them is crucial for writing efficient code, especially when working with move semantics and perfect forwarding.

Quick Reference Guide

Expression
lvalue
xvalue
prvalue

Variable name

Function returning T&

Function returning T&&

Function returning T

std::move(x)

Literal (42, 3.14)

String literal "hello"

x + y

++x

x++

*ptr

Reminder:

  • lvalues have identity and persist beyond expressions

  • prvalues are temporary values without identity

  • xvalues have identity but are candidates for moving

  • glvalues (lvalue + xvalue) have identity

  • rvalues (xvalue + prvalue) can be moved from

Conclusion

Understanding C++ value categories is essential for modern C++ development. They're the foundation for move semantics, perfect forwarding, and optimization. While the terminology can seem daunting at first, these concepts enable you to write more efficient, expressive code.

Last updated