Reputation: 6390
Where in the C++ Standard is it stated that a const rvalue reference doesn't bind to an lvalue?
For example the code below doesn't compile:
#include <iostream>
int i = 10;
int f(const int&& j) { return j; }
int main()
{
std::cout << f(i) << '\n';
}
Upvotes: 1
Views: 127
Reputation: 39151
In the last bullet of [dcl.init.ref]/5 (quoting n3485):
If
T1
[the type of the initialized reference] is reference-related toT2
[the type of the initializer expression] and the reference is an rvalue reference, the initializer expression shall not be an lvalue.
The cv-qualification is irrelevant in this case.
The first (main) bullet of /5 doesn't apply since the reference is not an lvalue reference:
If the reference is an lvalue reference and the initializer expression [...]
The second (main) bullet point applies:
Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be
const
), or the reference shall be an rvalue reference
[emphasis mine]
The first sub bullet point of this doesn't apply since the initializer is is not an xvalue or function prvalue and doesn't have class type.
The second sub-bullet-point is an unconditional "otherwise", so that last sub bullet point applies.
If the initializer is not reference-related to the referred type (T1
), the example compiles:
#include <iostream>
double i = 10;
int f(const int&& j) { return j; }
int main()
{
std::cout << f(i) << '\n';
}
Upvotes: 6
Reputation: 385405
[C++11: 8.5.3/3]
lists how initialisers for references work; what you're looking for is there. It's too exhaustive to quote here verbatim, though.
Upvotes: 3