Logical const

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 30 05:04:57 PST 2010


On Mon, 29 Nov 2010 17:21:27 -0500, Simen kjaeraas  
<simen.kjaras at gmail.com> wrote:

> Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>
>> Except the language says what results from your code (casting away  
>> const and then mutating) is undefined behavior.  This means all bets  
>> are off, it can crash your program.  I'm unsure how the compiler could  
>> take that route, but that's what's in the spec.
>>
>> Maybe because of the way const works, it never really is undefined, but  
>> there will always be that loophole.
>
> The thing is, immutable is implicitly castable to const, and immutable
> data could be stored in write-protected memory. Apart from that, I  
> believe
> it is safe to cast away const.

One would have to ensure that data with mutable members never makes it  
into ROM.  This should be easy for the compiler since the full type is  
always known at construction time.

There are other issues with logical const that would need to be addressed,  
specifically pure functions and implicit sharing.

-Steve


More information about the Digitalmars-d mailing list