Logical const

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


On Tue, 30 Nov 2010 08:04:57 -0500, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> 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.

I should clarify that this only applies if logical const is a language  
feature, not a library feature.  So in the context of this sub-thread, you  
are right, one needs to be careful.

-Steve


More information about the Digitalmars-d mailing list