Casting away const

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 9 08:10:45 PDT 2010


On Mon, 09 Aug 2010 11:04:40 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Mon, 09 Aug 2010 10:53:48 -0400, BCS <none at anon.com> wrote:
>
>> OTOH that is effectively a hidden cast and has 100% of the same issues  
>> (re undefined behavior) as casting away const while being slightly  
>> harder to find.
>
> But you just said that casting and reading is not undefined?  Isn't this  
> the same thing?
>
> Const is such a strange beast because it plays no role in code  
> generation, it's effectively only a tool to help the compiler decide  
> what is possible.  It doesn't occupy any space or translate whatsoever  
> to the underlying code.
>
> I think there are definite good uses for writing to const or immutable  
> data besides ones that can be stored in ROM.  That is, if you are sure a  
> const or immutable piece of data is on the heap/stack, it should be  
> reasonable to be able to modify it for performance gains.

I should say, when performance gains are not possible because of the  
limitations of the const/immutable notation.  The classic example is the  
mutable cache for avoiding recalculations on an immutable object.

-Steve


More information about the Digitalmars-d-learn mailing list