I understand why int& x=1
is illegal(you can't have reference to constant value), but I don't understand why const int& x=1
is legal? How can you have reference to the number "1"? It is not even a defined variable.
EDIT: I read the answer given in this post: What happens when I assign a temporary int to a const reference in C++? but can someone explain what does he means "The lifetime of the temporary value returned by the expression f(1) will have its lifetime extended. This rule is unique for const references."? so literals are stored as int on the stack or not? Thanks!