L-Values and R-Values
Expressions in C++ can evaluate to l-values or r-values. L-values are expressions that evaluate to a type other than void and that designate a variable.
L-values appear on the left side of an assignment statement (hence the "l" in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values.
The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.
Some examples of correct and incorrect usages are:
// lValues_rValues.cpp
int main() {
int i, j, *p;
i = 7; // OK variable name is an l-value.
7 = i; // C2106 constant is an r-value.
j * 4 = 7; // C2106 expression j * 4 yields an r-value.
*p = i; // OK a dereferenced pointer is an l-value.
const int ci = 7;
ci = 9; // C3892 ci is a nonmodifiable l-value
((i < 3) ? i : j) = 7; // OK conditional operator returns l-value.
}
备注
The examples in this section illustrate correct and incorrect usage when operators are not overloaded. By overloading operators, you can make an expression such as j * 4 an l-value.