Reputation: 1577
In my understanding, an lvalue is just a location, and its corresponding rvalue is the value stored at that location. for example:
int x;
x = 0; /* the compiler will replace x with the location where 0 will be stored */
int y = x; /* x works as an rvalue here, its content is unknown until run time */
Can the compiler known at compile time that the value of x is 0 in the third line, so that it can initialize y with the value 0 directly (instead of wait until run time, and at run time first get the value of x then give that value to y)?
int *p = &x;
*p = 3;
Here *p in the second line is a lvalue (the location of x). Since &x is known at compile time, so *p is also known at compile time, right?
Will the compiler simply replace *p with x's location, or will it generate code that first get the address stored in p, then assign 3 to that address?
Upvotes: 1
Views: 254
Reputation: 7962
I'm not sure what you want has to do with lvalues or rvalues. Correct me if I'm wrong, but I'm sensing you want to do some "manual optimizations" by ensuring, coarsely speaking, that things that are determined at compile-time don't take any computation at run-time. There are many ways to do that, some more complicated than others.
From the two examples that you posted, I would advise that you take an interest in const and static qualifiers, and in the new constexpr key-word in C++11. Generally speaking, never forget to write const
when you can; it really helps the compiler during optimization. For static
it's another story; it turns out to be safer for everyone if you use it only if you are already quite experienced in programming, only in some specific cases, and some are religiously against using it altogether...
If you want to know a little more about optimization, I would recommend this website, and of course there is also template-meta-programming which can help make some computation happen at compile-time.
To go back to your example, why would you write such a thing and expect the compiler to do the work for you? If you know y
is 0, why not write it yourself? If the value of y
is going to change later on, how would you do otherwise than allocating space for y
in memory, and copy the value 0 at this location when your code "starts" using it (at runtime, that is)? What if the value of y
was set depending on a condition that could only be resolved at run-time? Etc.
Optimization is really exciting, true, but it should not come first in a development. If you have programmed something, and that you think it could run faster, then ask yourself how, but most of the time, it's useless (and dangerous, and inefficient) to try and optimize each instruction.
"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity.", W.A. Wulf
Upvotes: 2
Reputation: 42142
I would not say that in the expression int y = x
the variable x
is an r-value. The fact that it is on the right-hand side does not mean that it is an r-value.
By definition, an l-value is an object that occupies some identifiable location in memory, such as x
; r-values are simply objects which are not l-values.
So if the expression were int y = x1 + x2
, then, yes, the temporary x1 + x2
would be an r-value, but in your case, and for the lifetime of the scope in which x
is defined, x
has an identifiable location in memory and is thus an l-value.
Upvotes: -2