I understand that compilers can optimize references out of existence so that they don't "occupy storage" (
http://en.cppreference.com/w/cpp/language/reference ). For example, this is why the outputs below show identical addresses.
1 2 3 4 5 6 7
|
//In other words, the 'y' variable below, despite possibly being a lvalue,
//does not cause the compiler to allocate space to store its value
//(i.e., the reference here is not implemented as a pointer of sorts)
int x;
int &y = x;
std::cout << &x << endl;
std::cout << &y << endl;
|
1) Could someone confirm that this never happens to rvalue references (i.e., rvalue references such as the 'z' variable below will always occupy storage)?
-----------------------------------------------------------------------------
In addition:
I think I've seen C++11/14 tutorials state that b/c we can take the address of a rvalue reference variable, such variables are themselves lvalues.
2) Can the same be said of lvalue references? For example, the lvalue reference variable 'y' above is itself a lvalue, even though it does not occupy storage.
This appears to be true b/c any qualifying expressions can be a lvalue (
http://en.cppreference.com/w/cpp/language/value_category ); such expressions do not necessarily need to be objects nor occupy storage (for example, even a string literal can be a lvalue, even though I've never seen it used on the left side of the = operator).
Thanks