Wrong form:
int &z = 12;
Correct form:
int y;
int &r = y;
Question:
Why is the first code wrong? What is the "meaning" of the error in the title?
cpointersreference
Wrong form:
int &z = 12;
Correct form:
int y;
int &r = y;
Question:
Why is the first code wrong? What is the "meaning" of the error in the title?
Best Answer
C++03 3.10/1 says: "Every expression is either an lvalue or an rvalue." It's important to remember that lvalueness versus rvalueness is a property of expressions, not of objects.
Lvalues name objects that persist beyond a single expression. For example,
obj
,*ptr
,ptr[index]
, and++x
are all lvalues.Rvalues are temporaries that evaporate at the end of the full-expression in which they live ("at the semicolon"). For example,
1729
,x + y
,std::string("meow")
, andx++
are all rvalues.The address-of operator requires that its "operand shall be an lvalue". if we could take the address of one expression, the expression is an lvalue, otherwise it's an rvalue.