Consider the following C++11 code:
int& f();
int&& i = f();
This produces an error:
cannot bind ‘int’ lvalue to ‘int&&’
I understand why, but I'm trying to justify it from the language in the standard.
Paraphrasing C++11 N3485 8.5.3.5:
A reference to int is initialized by an expression of type int as follows: (yes)
If the reference is an rvalue reference: (yes)
If the initializer expression: (yes)
- is an xvalue, class prvalue, array prvalue or function lvalue (no?), or
- has class type [snip] (no)
Is it the case that neither of these two points apply? So we should just assume it is ill-formed because neither do?
How can you determine from the standard that you can't initialize an rvalue reference to int with an lvalue of type int?
std::move
exists. – chrisf
rather thanf()
? – jogojapanstd::move
returns an rvalue reference, which when called as a postfix function call expression is an xvalue expression. – Andrew Tomazos