Possible Duplicate:
Is const_cast safe?
Obviously I'd never write this code, but it's a very much simpler example of something that came up in a real program.
#include <iostream>
void change(const int& data)
{
int& data2 = const_cast<int&>(data);
data2 = 100;
}
int main()
{
int thing = 123;
change(thing);
std::cout << thing << "\n";
}
Is it well defined behavior that this alters the data referred to, or is the compiler allowed to assume that because it's passing a const int&
that the function can't change the value passed in and generate code accordingly?
edit: All the compilers I tried it on output the changed value, 100.
This appears to be a duplicate of Can C++ compiler assume a const bool & value will not change? so I'm happy to close this one.