Store mutable indirections in immutable data with this one weird trick!

Timon Gehr timon.gehr at gmx.ch
Sat Nov 13 07:20:37 UTC 2021


On 13.11.21 07:50, Paul Backus wrote:
> 
> What do you think? Is it just crazy enough to work, or just plain crazy?

Second option.

> Is there some fatal safety violation I've overlooked?

It does not matter how you do it if the compiler assumes it does not 
happen...

DMD 2.098.0:

```d
void main()@safe{
     immutable TailUnqual!(int*) p = new int(123);
     auto mut = p.ptr;
     immutable immut = p.ptr;
     import std.stdio;
     writeln(*immut); // 123
     *mut=0;
     assert(mut is immut);
     assert(*mut == *immut);
     writeln(*mut," ",*immut); // 0 123
}
```


More information about the Digitalmars-d mailing list