C++ VALUE CATEGORY HIERARCHY:
Expressions
/ \
glvalue rvalue
/ \ / \
lvalue xvalue prvalue
(Identity, (Identity, (No Identity,
Not Movable) Movable) Movable Temporary)Expressions in C++: Operands, Operators, and Evaluation
Direct Answer & Core Concept
Detailed Explanation & Mechanics
1. Value Categories Taxonomy (C++11 and onwards):
2. Expression Evaluation & Short-Circuiting:
Logical AND (&&) and Logical OR (||) evaluate operands from left to right with guaranteed short-circuit evaluation: if the left operand determines the outcome, the right operand is never executed.
Runnable Code Example: Expressions in C++
C++20 / C++23#include <iostream>
#include <string>
#include <utility>
void processValue(const std::string& lval) {
std::cout << "Lvalue bind: " << lval << "
";
}
void processValue(std::string&& rval) {
std::cout << "Rvalue / Move bind: " << rval << "
";
}
int main() {
std::string message = "Persistent lvalue";
processValue(message); // Calls lvalue overload
processValue("Temporary prvalue"); // Calls rvalue overload
processValue(std::move(message)); // std::move converts to xvalue -> rvalue overload
// Short-circuit evaluation demonstration
int* ptr = nullptr;
if (ptr != nullptr && *ptr == 10) {
// Safe: *ptr is never evaluated because left side is false
} else {
std::cout << "Short-circuit evaluation prevented null pointer dereference.
";
}
return 0;
}Common C++ Technical Interview Questions & Answers
Interview PrepQ: What is the difference between an lvalue and an rvalue in C++?
Q: Why is short-circuit evaluation critical for pointer safety?
Key Takeaways, Modern Best Practices & Idioms
C++ Best Practices & Production Rules
2. Use
std::move to explicitly cast lvalues to xvalues when ownership transfer is intended.3. Avoid expressions with side-effects on the same variable without sequence points (e.g.
i = i++).