Casting away immutability
Sergei Degtiarev via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 1 19:04:58 PDT 2015
I can't understand how cast coexist with immutability. Consider
the code:
immutable immutable(int)[4] buf;
auto x=buf[0];
auto p=buf.ptr;
auto i=cast(int[]) buf;
i[]=1;
assert(buf.ptr == p);
assert(buf[0] != x);
I've just modified immutable data, is it normal?
Another, ever simpler, code:
immutable a=0;
auto b=cast(int*) &a;
*b=1;
assert(&a == b);
assert(a != *b);
Last two assertions both succeed and really puzzle me. What is
pointer in D at all?
More information about the Digitalmars-d-learn
mailing list