Interested in D, spec confuses me.
Dicebot via Digitalmars-d
digitalmars-d at puremagic.com
Tue Feb 2 16:43:36 PST 2016
On 02/03/2016 02:31 AM, bubbasaur wrote:
> import std.stdio;
>
> struct Data { int x; }
> auto func1(Data* d) { return d.x; }
> auto func2(const(Data)* d) { return d.x; }
> auto func3(immutable(Data)* d) { return d.x; }
>
> void main() {
> Data d;
> auto value1 = d.x*func1(&d) + d.x;
> auto value2 = d.x*func2(&d) + d.x;
> auto value3 = d.x*func3(cast(immutable)&d) + d.x;
> d.x++;
> writeln(d.x++);
> }
>
> Functions 2 and 3 are acting in the same way and different from what you
> said I can change the d.x in main.
Casting immutable and violating it is undefined behavior and once you do
it all bets are off - compiler is even allowed to attempt formatting
your hard drive when compiling this program :)
The fact DMD doesn't do anything scary right now when you do it is a
mere coincidence and may change in any release with no notice.
More information about the Digitalmars-d
mailing list