The draft C++ standard section 7.1.6.1
The cv-qualifiers paragraph 4 says:
[...]any attempt to modify a const object during its lifetime (3.8) results in undefined behavior
So any behavior is possible but you should not be doing this and you definitely can not rely on this behavior. Of course const_cast does have valid uses as the accepted answer in Is const_cast safe? says:
const_cast is safe only if you're casting a variable that was originally non-const.[...]
We can see one way the results you are seeing can happen from this live example that gcc 4.8.1
without any optimization is just using the value 5
instead of reading the current value:
movl $7, (%rax)
movl $5, %esi
movl $_ZSt4cout, %edi
in the non const
case we will see something like this:
movl $7, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %esi
movl $_ZSt4cout, %edi