Well that is kind of true. All temporary file does have a home, because i just tried that
int* foo(const int& x) { int* t = (int*)&x; *t=22; return t; } void main() { int* ref = foo(12); cout<<ref<<endl; //This is stil pointing to the temporary variable address. }
That's undefined behavior right there. I don't think you can rely on undefined behavior to come to any kind of meaningful conclusions about what the standard guarantees.
And i was a able to modify 12 to 22. Also when i try printing the address of 12 it does print out an Address. The interesting thing is that the return value when i print it the address is still valid. The thing that was very interesting though is that if you look at the Assembly you will notice that the value that is assign to foo is still valid in the assembly code.
Without trying to get super technical in this thread, temporaries may or may not have addresses associated with them, as they can potentially be completely optimized away or live entirely in a register instead of in memory. But yes, as you have noticed, you can pass a temporary or literal by const reference, which implies it's got some address or memory location associated with it (this is most easily proven by simply taking and printing the address of the const reference). A situation like this would fall under the "the temporary is just a guest in a temporary home" situation I described at the end. It's also important to note that when you print out that address, it is not still valid (that is, the temporary no longer exists in any way, shape, or form (as far as the standard guarantees), so that address you're printing out is just the temporary's old address, not a currently valid address (because the temporary doesn't even exist... how can you address it?))