Sungmin
Sungmin

Reputation: 2631

Why Rvalue cannot bind Lvalue reference?

This is a simple and old question. I am sure that it must be asked somewhere else. But after over 30 minutes of googling, I decided to ask here again. The flood of information about rvalue reference makes me hard to find proper web pages for this issue.

What is the reason behind this decision that rvalues cannot bind lvalue references?

Upvotes: 5

Views: 3026

Answers (1)

edmz
edmz

Reputation: 8494

Rvalues are usually something about to die or a literal. If this code were legal:
int &r = 5
then you would be able to modify 5, but that doesn't make sense! A reference (of any kind) is just an alias for the referenced object.
However, lvalue references to const forbid any change to the object and thus you may bind them to an rvalue.
const int &r = 5; // ok r = 10; // compilation error

Upvotes: 6

Related Questions