Vision for the D language - stabilizing complexity?
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jul 13 05:49:34 PDT 2016
On 07/13/2016 08:15 AM, ag0aep6g wrote:
>
> ----
> struct S { immutable int a; int b; }
>
> immutable(int*) f() pure
> {
> S* s = new S;
> return &s.a;
> }
>
> void main()
> {
> immutable int* p1 = f();
> S* ps1 = cast(S*) p1;
> ps1.b = 42;
>
> immutable int* p2 = f();
> S* ps2 = cast(S*) p2;
> ps2.b = 43;
> }
> ----
>
> f is marked pure, has no parameters and no mutable indirections in the
> return type. So f is strongly pure. That means, the compiler is free to
> reuse p1 for p2. Or it may allocate two distinct structures, of course.
>
> So, ps1 may or may not be the same as ps2, if those casts are allowed.
> That can't be right.
Good example. I think the two pointers should be allowed to be equal. --
Andrei
More information about the Digitalmars-d
mailing list