[Issue 21690] Unable to dynamic cast extern(C++) classes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Mar 11 02:17:03 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21690

--- Comment #10 from thomas.bockman at gmail.com ---
(In reply to Basile-z from comment #9)
> dynamic cast of c++ classes can work to a **very** limited extent that is if
> the target type is **exactly** the same (i.e not one of its derivate). This
> can be done by comparing the virtual table address of the instance with the
> virtual table address of the target type.

Cool! That actually covers a lot of use cases, and is better than nothing.
Maybe I'll publish a dub package with a more fleshed-out version of it
eventually.

However, it seems a little too unpredictable and limited to be worth
implementing in the compiler itself, given that kinke mentioned earlier that
two instances of the same class may still end up pointing to different vtable
addresses if they were instantiated on different sides of the language barrier:

(kinke from comment #1)
> But then again, when we are confronted with some C++ class ref, we have no idea
> whether it was instantiated on the D side or C++ side and so which vtable its
> vptr points to.

--


More information about the Digitalmars-d-bugs mailing list