immutable/mutable aliasing

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 3 14:25:32 PDT 2014


On Thursday, 3 July 2014 at 20:43:33 UTC, Jet wrote:
> void foo(immutable int* x, int* y) {
>  bar(*x); // bar(3)
>  *y = 4;  // undefined behavior
>  bar(*x); // bar(??)
> }
> ...
> int i = 3;
> foo(cast(immutable)&i, &i);
> ----------------------------------
> In the 2.065 version, I can compile. But that is not in the 
> documentation.

It's undefined behaviour, which means: don't do it. Your sample
compiles, because you're casting. By casting you're saying "shut
up compiler, I know what I'm doing".

See also: http://dlang.org/const3.html - "Removing Immutable With
A Cast"


More information about the Digitalmars-d-learn mailing list