Casting away const

BCS none at anon.com
Mon Aug 9 08:00:36 PDT 2010


Hello Steven,

> On Mon, 09 Aug 2010 10:24:56 -0400, BCS <none at anon.com> wrote:
> 
>> Hello Steven,
>> 
>>> On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo
>>> <simen.endsjo at pandavre.com>  wrote:
>>>> I'm totally new to the const/immutable thing, so this might be a
>>>> naive  question..
>>>> The spec says:
>>>> "modification after casting away const" => "undefined behavior"
>>> I thought it was "you're on your own", not undefined behavior.  The
>>> former  implies there is some "right" way to do this if you know
>>> more about the  data than the compiler, the latter implies that
>>> there is no right way to  cast away const.  Am I wrong?
>>> 
>> I think you are right re the meaning of those terms but I think what
>> you  thought it to be is more along the lines of what is correct. I
>> /think/  the situation is that there are things that will work
>> reliably and  things that result in undefined behavior and you're on
>> your own figuring  out what is what.
>> 
> I'm sort of interpreting what you're saying is:
> 
> In some cases, you can cast away const and modify the variable with no
> adverse effects, but we can't tell you which cases those are.

Yes and no. Yes, sometimes casting away const and then writing to the pointer 
doesn't crash your program (or do anything else) but no that's not what I 
was saying. It's still undefined behavior even if it "works". What "your 
on your own" with is making sure you don't write to the pointer. Once you 
cast, the compiler won't check your work.

> 
> Which is kind of like saying "yes, casting away const is allowed, but
> only  people who wrote the compiler can do it".
> 
> -Steve
> 
-- 
... <IXOYE><





More information about the Digitalmars-d-learn mailing list