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?
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")
, and x++
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.
&obj; // valid
&12; //invalid
int & = 12;
is invalid, the standard says a string literal is an lvalue, other literals are rvalues.
std::string("meow")
constructs an object of type std::string
and yields a rvalue that designates this object, 1729
has no side-effect and yields value 1729 as an rvalue of type int
.
Commented
Nov 29, 2011 at 2:42
"Lvalues name objects that persist beyond a single expression."
is a 100% correct statement. On the contrary, your example (const int &)1
is incorrect, because It is NOT a "named" object.
Commented
Nov 29, 2011 at 12:19
int &z = 12;
On the right hand side, a temporary object of type int
is created from the integral literal 12
, but the temporary cannot be bound to non-const reference. Hence the error. It is same as:
int &z = int(12); //still same error
Why a temporary gets created? Because a reference has to refer to an object in the memory, and for an object to exist, it has to be created first. Since the object is unnamed, it is a temporary object. It has no name. From this explanation, it became pretty much clear why the second case is fine.
A temporary object can be bound to const reference, which means, you can do this:
const int &z = 12; //ok
For the sake of the completeness, I would like to add that C++11 has introduced rvalue-reference, which can bind to temporary object. So in C++11, you can write this:
int && z = 12; //C+11 only
Note that there is &&
intead of &
. Also note that const
is not needed anymore, even though the object which z
binds to is a temporary object created out of integral-literal 12
.
Since C++11 has introduced rvalue-reference, int&
is now henceforth called lvalue-reference.
12
is a compile-time constant which can not be changed unlike the data referenced by int&
. What you can do is
const int& z = 12;
void f( vector<int> const & )
, which is idiomatic to pass a vector that is not to be modified. The problem now is that f( vector<int>(5) )
would be incorrect, and the user would have to provide a different overload void f( vector<int> v ) { f(v); }
which is trivial.
Commented
Nov 28, 2011 at 13:20
f( vector<int>(5) )
, the compiler creates a temporary and then binds the reference to that temporary, and similarly if there was an implicit conversion from 5
directly. This allows the compiler to generate a single signature for the function and enables a single user implementation of the function. From there on, similar behaviour is defined for the rest of the uses of constant references for consistency.
Commented
Nov 28, 2011 at 13:23
T const & r = *ptr;
, any later use of r
in the function can be replaced by *ptr
, and r
does not need to exist at runtime) or it might have to be implemented by keeping the address of the object that it aliases (consider storing a reference as a member of an object) --which is implemented as an autodereferenced pointer.
Commented
Nov 28, 2011 at 13:27
These are the rules of the C++ language:
12
) is a "rvalue"int &ri = 12;
is ill-formedYou have to understand that these are C++ rules. They just are.
It is easy to invent a different language, say C++', with slightly different rules. In C++', it would be permitted to create a non-const reference with a rvalue. There is nothing inconsistent or impossible here.
But it would allow some risky code where the programmer might not get what he intended, and C++ designers rightly decided to avoid that risk.
References are "hidden pointers" (non-null) to things which can change (lvalues). You cannot define them to a constant. It should be a "variable" thing.
EDIT::
I am thinking of
int &x = y;
as almost equivalent of
int* __px = &y;
#define x (*__px)
where __px
is a fresh name, and the #define x
works only inside the block containing the declaration of x
reference.
(ostringstream() << "x=" << x).str()