Rvalue to lvalue conversion?

user8194556 picture user8194556 · Jun 21, 2017 · Viewed 9k times · Source

Why it is not possible to convert rvalues to lvalues? It is possible to do a conversion in the opposite direction though. Technically rvalues do have a memory address, isn't it?

Answer

sp2danny picture sp2danny · Oct 4, 2018

You can:

int&& x = 3;

x is now an lvalue. A so called 'rvalue-reference' can bind to a temporary, but anything with a name is an lvalue, so you need to forward<>() it if you need it's rvalueness back.

Note that by binding a temporary to a rvalue-reference (or a const reference) you extend its lifetime. Technically a cast is possible, but not recommended, since temporaries have short lifetime, so you typically get a dangling reference.