const?? When and why? This is ugly!

Sergey Gromov snake.scaly at gmail.com
Sun Mar 8 17:21:26 PDT 2009


Sun, 08 Mar 2009 13:01:27 -0200, Ary Borenszweig wrote:

> As I see it, the cast should fail saying "can't cast a to invariant 
> because a is mutable", because otherwise you are producing a behaviour 
> that doesn't match what the code says.
> 
> In:
> 
> char c;
> int* p = cast(int*)&c;
> *p = 5;
> 
> there's nothing wrong about the cast because a char can be viewed as an 
> int. But a mutable data cannot be seen as immutable.

It's not interpreting char as int.  It's interpreting c and part of
return address as an int on x86 essentially corrupting stack.  Or gets
an unaligned access hardware exception.  Or whatever.  It's anything but
a correct code.  It's exactly the same issue as casting mutable to
immutable: you're abusing the power the compiler gave you.

Though I agree that the line between safe and unsafe casts is rather
subtle.



More information about the Digitalmars-d mailing list