Why is it allowed to pass R-Values by const reference but not by normal reference?

For your final question:

how can a const reference keep pointing to an R-Value (anonymous variable)

Here is the answer. The C++ language says that a local const reference prolongs the lifetime of temporary values until the end of the containing scope, but saving you the cost of a copy-construction (i.e. if you were to use an local variable instead).


Think of any object as the box containing some value inside and the box may or may not have name tag, i.e. the box with name tag as variable and the box without name tag as literal. Whether name tag is there or not, we have the box.


Reference is the way that we add name tag to our box.

int a = 5;
int &b = a;

we have two name tags for our box (with value 5 inside).

const int &c = 5;

there you are, the box was just named.


The new name of the box that has never had a name before must be marked as const. Because the value inside the box can be changed through its name, which we do not want it to happen (not allowed to happen) to our literal box.


It gets back to the definition of literal. A literal is a constant; e.g.the value of the number 5 will not change, ever, though a variable may change from being assigned the value of 5 to another value. Passing a literal by reference implies that the function may modify it, which is something you can't do to a literal, by definition, which is why the language requires that you modify it with const. I don't think C++ could modify literals even if it let you try, but it still enforces this convention to remind the programmer that a literal value cannot be modified.

Hope that answers your question!