@safe - why does this compile?
Timoses
timosesu at gmail.com
Fri Jul 13 13:52:27 UTC 2018
On Friday, 13 July 2018 at 11:04:40 UTC, Piotr Mitana wrote:
> This code:
>
> import std.stdio;
>
> class X1 {}
> class X2 : X1
> {
> void run() @safe
> {
> writeln("DONE");
> }
> }
>
> void main() @safe
> {
> X1 x1 = new X1;
> X2 x2 = cast(X2) x1;
> x2.run();
> }
>
> is obviously wrong gets killed by OS's signal. Why is it @safe?
> I thought @safe should prevent such errors as well.
I suppose this is another good example of how casting can be
dangerous?
E.g. also:
immutable int i = 3;
int* j = cast(int*)&i;
assert(i == 3);
*j = 4;
assert(j == &i); // data occupies same address space
assert(i == 3 && *j == 4); // yet the values differ
More information about the Digitalmars-d-learn
mailing list