[Issue 1563] dynamic cast is not always performed
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Oct 9 20:02:18 PDT 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1563
------- Comment #2 from wbaxter at gmail.com 2007-10-09 22:02 -------
(In reply to comment #1)
> Reply to d-bugmail at puremagic.com,
>
> > http://d.puremagic.com/issues/show_bug.cgi?id=1563
> >
> > Summary: dynamic cast is not always performed
> > Product: D
> > Version: 1.022
> > Platform: PC
> > OS/Version: Windows
> > Status: NEW
> > Severity: normal
> > Priority: P2
> > Component: DMD
> > AssignedTo: bugzilla at digitalmars.com
> > ReportedBy: davidl at 126.com
> > class A{}
> > class B:A{}
> > class C:B{}
> > void main()
> > {
> > A a = new B;
> > C c = cast(C)cast(void*)a;
> > c = cast(C)c;
> > assert(c is null); // assertion failure
> > c = cast(C)cast(A)cast(void*)c; // workaround
> > assert(c is null);
> > }
> >
>
>
> it looks to me like the bug is that "cast(ClassType)voidPt" doesn't check
> the type. I think that assuming that something of type C actually is of type
> C should be allowed. The guards should be at the original assignment.
>
> OTOH that makes things with unions fun.
If you make cast(ClassType)voidPt check the type then D will have no way at all
to do a reinterpret_cast. And I also agree that assuming something of type C
is actually type C should be allowed. So I think the example is just bad code.
If you do a reinterpret_cast then you're taking responsibility into your own
hands.
But the danger involved would definitely be more apparent if there were a more
obvious construct for reinterpret casts like reinterpret_cast!(T).
--
More information about the Digitalmars-d-bugs
mailing list