The compiler does not check whether the function used to form a delegate mutates its context or not when declaring a manifest constant.
This should not work:
```
class C
{
int member;
int foo()
{
member++;
return 123 + member;
}
}
enum u = &(new C().foo);
void main()
{
import std.stdio : writeln;
writeln(u()); // 124
writeln(u()); // 125
writeln(u()); // 126
writeln(u()); // 127
}
```
This was found by this user: https://forum.dlang.org/post/[email protected]
The expected behavior would be the statement `enum u = &(new C().foo);` failing for the expression `&(new C().foo)` is not constant, akin to assigning to const.
`const u = &(new C().foo);` => `Error: expression &C(0).foo is not a constant`
Comment #1 by contact — 2018-11-05T00:01:53Z
Actually, `const u = &(new C().foo);` fails even if C.foo does not mutate its object and is annotated const.
So I would suggest failing only for non-const methods, though that may be a bigger change.
Comment #2 by robert.schadek — 2024-12-13T19:01:10Z