const?? When and why? This is ugly!
Walter Bright
newshound1 at digitalmars.com
Sun Mar 8 00:36:06 PST 2009
Derek Parnell wrote:
> import std.stdio;
> void main()
> {
> int [] a = new int [1];
>
> a [0] = 1;
>
> invariant (int) [] b = cast (invariant (int) []) a;
>
> writef ("a=%s b=%s\n", a [0], b[0]);
> a [0] += b [0];
> writef ("a=%s b=%s\n", a [0], b[0]);
> a [0] += b [0];
> writef ("a=%s b=%s\n", a [0], b[0]);
> }
>
> The problem is that we have declared 'b' as invariant, but the program is
> allowed to change it. That is the issue.
>
The following also compiles:
char c;
int* p = cast(int*)&c;
*p = 5;
and is clearly buggy code. Whenever you use a cast, the onus is on the
programmer to know what they are doing. The cast is an escape from the
typing system.
More information about the Digitalmars-d
mailing list