C++ Value Categories | lvalue, prvalue, xvalue Explained
이 글의 핵심
Map of C++11 value categories with examples tying move semantics and forwarding to expressions.
Classification
expression
├─ glvalue
│ ├─ lvalue
│ └─ xvalue
└─ rvalue
├─ prvalue
└─ xvalue
lvalue
Has a name and can have its address taken (with exceptions).
int x = 10;
int* p = &x;
int& r = x;
prvalue
Pure rvalue: temporaries, literals, results of many operations.
int y = x + 5; // x+5 is a prvalue
xvalue
“Expiring” value—typically from std::move, certain && returns, or subobjects of temporaries.
std::vector<int> v2 = std::move(v); // move(v) is an xvalue
Reference binding
const int& cr = 10; // OK: const& extends lifetime of temporary
int&& rr = 10; // OK: binds to prvalue
decltype and categories
int x = 10;
decltype(x) // int
decltype((x)) // int& — extra parens matter
Practical guidelines
- Prefer return by value and let RVO/
std::moverules apply; do notstd::movelocal variables being returned unless you know NRVO cannot apply and you intend a move. - Use forwarding references
T&&plusstd::forwardto preserve value category through wrappers.
FAQ
See the Korean original for additional overload-resolution and if constexpr category-print examples.
Related posts
- Perfect forwarding
- Move semantics
Keywords
C++, lvalue, prvalue, xvalue, value category