I have tested that the code even does not compile in Visual Studio 2008.
Another question is, why currently the compiler does not allow
binding a non-const reference to an rvalue? What is the internal
reason and benefits/risks behind this rule?
To avoid surprises like this:
void f(long& x) { x = 1; }
int y = 0;
f(y); // hypothetical, doesn't compile
assert(y == 1); // fails
If f(y) were allowed to compile, it would generate a temporary of type
long, and the function would modify that temporary, leaving the original
int variable unchanged.
It would also mean that a seemingly benign change in function
signature( from f(int&) to f(long&) ) may silently alter the behavior of
the program. As things stand now, such a change would lead to compiler
errors - much better than silent behavior change.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925