What should delegates with qualifiers mean?
Paul Backus
snarwin at gmail.com
Wed Mar 24 21:56:33 UTC 2021
On Wednesday, 24 March 2021 at 19:23:15 UTC, Q. Schroll wrote:
> While everywhere else, immutable is a requirement and a
> guarantee, here it is a guarantee only. That is because the
> context object cannot be reassigned independently of the
> function pointer. Therefore, a `delegate() immutable` is
> convertible to a `delegate() const` delegate which in turn is
> convertible to a `delegate()`.
No, this is unsound, because a delegate may return a pointer or
reference to its context. Consider:
struct S
{
int n;
int* getPtr() { return &n; }
}
void main() @safe
{
immutable S s = { 123 };
// implicitly convert from `delegate() immutable` to
`delegate()`
int* delegate() dg = &s.getPtr;
// undefined behavior
*dg() = 456;
}
More information about the Digitalmars-d
mailing list