Casting away const

BCS none at anon.com
Mon Aug 9 07:50:18 PDT 2010


Hello Steven,

> On Mon, 09 Aug 2010 09:57:47 -0400, bearophile
> <bearophileHUGS at lycos.com>  wrote:
> 
>> Steven Schveighoffer:
>> 
>>> 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?
>> In my opinion if this thing is well designed then you go in undefined
>> behaviour only when you change the contents of something after you
>> have  removed its const nature with a cast. Just casting const away
>> and then  reading the data can't be undefined behaviour, otherwise
>> casting const  away is useless and can be totally disallowed.
>> 
> Casting away const just to read the data is useless.  You can read
> const  data without a cast.
> 
> But my understanding is that casting away const to write should be
> doable  if you know what you're doing.  If this is undefined behavior,
> then that's  fine, I'm just unclear on what "undefined behavior"
> actually means.  I  thought "undefined behavior" means that you cannot
> count on the behavior  to work in the future.

Undefined behavior implies exactly that, once you enter the realm of undefined 
behavior, /anything/ that happens is your fault, the program can do anything 
at all and sill be considered a conforming program. It need not even behave 
the same between one run and the next.

The practical implications of this are that compilers are allowed to always 
assume the code never does anything that results in undefined behavior and 
generate code that is wrong in any number of ways if that assumption fails.

-- 
... <IXOYE><





More information about the Digitalmars-d-learn mailing list