On Friday, 17 August 2012 at 01:59:03 UTC, Chris Cain wrote:
On Friday, 17 August 2012 at 01:51:38 UTC, Mehrdad wrote:
So you're saying casting away a const _pointer_ is undefined, even if the target was originally created as mutable. (Otherwise, the code would certainly be "valid", just like in C++.)


Which means you can effectively _never_ cast away constness of a pointer/reference, no matter how certain you are about the target object, right?

If you did, then the code would be invalid, and the compiler could simply format your C: drive instead of modifying the object.

If so, then why is such an undefined cast allowed in the first place?

If you're absolutely 100% completely totally certain that the data is mutable (i.e., you have confirmed either through good, sound reasoning OR you have some method of seeing exactly where it is stored in your RAM and you've checked that the place it is stored is writable memory), then __technically__, yes you can cast away and modify away.





If doing so is "invalid" (i.e. undefined?) like you mentioned, no, you can't, because the compiler can't guarantee it can respect your assumptions and generate otherwise-correct code.

If it IS valid, then it seems to me like the situation is the exact same with C++.

Reply via email to