C++ mutable in D

Dukc ajieskola at gmail.com
Tue Aug 3 15:33:34 UTC 2021


On Tuesday, 3 August 2021 at 14:01:20 UTC, Paul Backus wrote:
> On Tuesday, 3 August 2021 at 13:02:38 UTC, Dukc wrote:
>>
>> Now, I definitely don't recommend using this. It might well 
>> end up being considered as a bug, and thus stop working in the 
>> future. Also the optimizer might not take this possibility 
>> into account, thus injecting bugs to your code.
>
> It's a known bug, since 2008:
>
> https://issues.dlang.org/show_bug.cgi?id=1983

I don't think it's that one. I'm not abusing the delegates 
context pointer mutablity, I'm abusing the fact that a `const` or 
`immutable` `delegate` may have a mutable return type:

```d
@safe:

void main()
{ import std;
   auto varArr = [5];

   immutable del = () => varArr[0];
   del().writeln;
   varArr[0] = 10;
   del().writeln;
}
```


More information about the Digitalmars-d mailing list