C++ Value Categories and Semantics

image.png

Types of Values

  • lvalue: ++x, x = 1, cout << '...', "hello world"
  • xvalue (expiring value): std::move(ptr1) can be considered as a named rvalue, distinguished from unnamed pure rvalue (prvalue)
  • prvalue: smart_ptr ptr1{new circle()}, return circle()

Value Categories

C++ has unique concepts of value categories - lvalue/rvalue:

Value and Reference Types Comparison

LanguageValue TypesReference Types
C++All primitive types, enums, structs, unions, classesReferences (&) and pointers (*)
JavaPrimitive types (numbers, etc.)Classes
PythonNoneAll

C++ primarily uses value semantics - parameters are passed by value (copied) by default.

Licensed under CC BY-NC-SA 4.0