You cannot bind a non-constant link to an rvalue, that is, you cannot βlinkβ to something that cannot appear on the left side of an assignment operator (for example, a temporary object). The main reason is that when temp is destroyed, you end up in a wrapped link, and C ++ does not allow this. Your code will not compile.
It's funny, however, that you can bind temp to a constant reference, and this will extend the lifetime of temp, i.e.
const Foo& ref = Foo();
However, the temporary is not destroyed. See Does a reference to a constant contain a temporary service life? However, I would not use this in my code, it's just a source of confusion for most of us.
source share