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. Apparently, according to some, it's
necessary for low level programming. I'd highly discourage this
type of behavior because if you're doing something like that I'm
nearly certain you could come up with a better design, not to
mention you're missing the point of having something const in the
first place.
Let's just say I wouldn't use const in a place where I'm itching
to cast it away. If you're not going to use it to your advantage,
there's no point.