Is this a violation of const?

Salih Dincer salihdb at hotmail.com
Fri Jul 29 23:15:14 UTC 2022


On Friday, 29 July 2022 at 21:56:20 UTC, Andrey Zherikov wrote:
> In the example below `func` changes its `const*` argument. Does 
> this violates D's constness?
>


It's smart to use `delegate`, but `immutable` doesn't necessarily 
mean `const`.  So if we use `const char`:

```d
struct S
{
     char s;

     void delegate(char s) update;
}

void func(const S* s)
{
     writeln(*s);
     s.update('D');
     writeln(*s);
}

import std.stdio;

void main()
{
     auto s = S('C');
     s.update = (_) { s.s = _; };

     writeln(s);
     func(&s);
     writeln(s);
} /* Prints:
S('C', void delegate(char))
const(S)('C', const(void delegate(char)))
const(S)('D', const(void delegate(char)))
S('D', void delegate(char))
*/
```
SDB at 79


More information about the Digitalmars-d-learn mailing list