Is this a violation of const?

ag0aep6g anonymous at example.com
Sat Jul 30 06:04:16 UTC 2022


On 29.07.22 23:56, Andrey Zherikov wrote:
> In the example below `func` changes its `const*` argument. Does this 
> violates D's constness?

Yes. Here's a modified example to show that you can also violate 
`immutable` this way:

struct S
{
     string s;
     void delegate(string s) @safe update;
}

S* makeS() pure @safe
{
     auto s = new S("test");
     s.update = (_) { s.s = _; };
     return s;
}

void main() @safe
{
     immutable S* s = makeS();
     assert(s.s == "test"); /* passes */
     s.update("func");
     auto ss = s.s;
     assert(ss == "test"); /* fails; immutable data has changed */
}


More information about the Digitalmars-d-learn mailing list