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.


Because literals are constant. 1 cannot become 2, and "abd" cannot become "edf".

If C++ allowed you to take literals by non-const reference, then it would either:

  1. Have to allow literals to change their meaning dynamically, allowing you to make 1 become 2.
  2. Expect the programmer to take extra care to modify values only via those references which do not refer to literals, and invoke undefined behaviour if you get it wrong.

(1) would create chaos in your program, because x == 1 could mean "is x equal to 2" depending on the context, and (2) is impossible to achieve because how is void display(int& a) supposed to know if it receives a literal reference or not?

Since neither of these two options makes sense, literals can only be passed by const references.


Actually, the deprecated conversion from string literals to char* is a good example of why the rules make a lot of sense, although it's not a reference but a pointer. You are allowed to let a char* point to "abc", but an attempt to actually utilise the "modifiability" attribute of the char* and modify one of the char elements results in undefined behaviour. This makes the whole deprecated conversion both dangerous and useless (in non-legacy code).

You wouldn't want to have such troubles in all other parts of the language, would you?