How does inheritance and vtables work wrt. C++ and interop with D? Fns w/ Multiple-inheritance args impossible to bind to?
evilrat
evilrat666 at gmail.com
Tue May 25 19:08:40 UTC 2021
On Tuesday, 25 May 2021 at 18:12:27 UTC, Gavin Ray wrote:
> On Tuesday, 25 May 2021 at 18:03:00 UTC, evilrat wrote:
>> That last one with someInt is what I warned about. D ctor
>> messed up class layout, in this simple case where class data
>> isn't used it almost works, but will be practically unusable
>> in real scenarios.
>
> Ah =/
> You have this in your code example:
> ```d
> static assert(Derived.someInt.offsetof == 16); // that's
> important otherwise D ctor will mess up everything
> ```
>
> Would this fix it, or is it just not super viable to hack
> around C++ multiple inheritance in D?
>
> Maybe generating some wrapper C++ code to be linked could help
> too?
> I'm not sure though/don't really know enough to say
Both this features uses mixins, but does conceptually different
things, my example with manual vtable casts is simpler but more
mechanical, and it basically mimics how it actually works.
Anyway the problem is with D side, compiler emits incorrect type
info/layouts (no wonder, it is clearly stated multiple
inheritance not supported), and then everything explodes.
So one of the possible workarounds is to separate class layout
and API with mixins/templates. No idea how it will affect
usability though, it's quite possible that extending such
horrible construct from D side would be cumbersome, too much PITA
to do real job using that thing.
Hard to say for sure without experiments.
Also like I said, D already does half of the work, it just needs
to collect all base classes and arrange vtable. One of the
potential blockers could be frontend architecture, I don't know
how much stuff such change would break, I think pretty much
everything was designed with single base class in mind.
Like this one, it has 'this' pointer, ctor/dtor and fields, but
no mention about layouts anywhere.
https://github.com/dlang/dmd/blob/master/src/dmd/aggregate.d#L86
The closest what it could be is
https://github.com/dlang/dmd/blob/master/src/dmd/aggregate.d#L279
More information about the Digitalmars-d-learn
mailing list