Unexpected behavior when casting away immutable

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 23 07:53:24 PDT 2015


On Wednesday, 23 September 2015 at 14:34:07 UTC, bachmeier wrote:
> On Wednesday, 23 September 2015 at 05:24:05 UTC, John Colvin 
> wrote:
>
>> violating immutable is undefined behaviour, so the compiler is 
>> technically speaking free to assume it never happens. At the 
>> very least, neither snippet's result is guaranteed to show a 
>> change or not. At the most, literally anything can happen.
>
> I was not aware that you could "violate" immutable. In that 
> case, it's not immutable.

immutable is guaranteed to be enforced at the type-system level. 
If you deliberately break the type system and tell the compiler 
to modify data that in actual fact is immutable, then that is 
undefined behaviour. If you're lucky, the data could have some 
protection such that writing to it will trigger a fault at the 
hardware level, but that's definitely *not* guaranteed.


More information about the Digitalmars-d-learn mailing list