In C++11 a const lvalue reference can be initialized with a mutable rvalue reference. The value referred to by the rvalue can then change producing a visible mutation to what the const lvalue is referring to. Here is an example:
int && rval = 3;
const int & lval = rval;
cout << "lval = " << lval << endl;
cout << "rval = " << rval << endl;
rval ++;
cout << "lval = " << lval << endl;
Output (from clang 3.2 and gcc 4.8.2 both with -std=c++11):
lval = 3
rval = 3
lval = 4
I would guess the reason for this is that the referent cannot be modified through the lvalue reference but it can be modified through the rvalue reference. But, I don't understand why a const lvalue is allowed to refer to a mutable object.
Can someone explain the rationale for this and give best practices for when dealing with such a situation. Also, are there other similar examples where constness can be subverted?