I just encountered a gotcha where D doesn't support extern(C++) classes with no vtable.
In C++, if a class has no virtual functions, there is no vtable.
If a virtual is added to a derived class, the vtable is pre-pended to the base.
Is it possible to replicate this in extern(C++)?
We have had a bunch of gotchas related to this.
You can argue that a base class with no virtual functions is a struct, and that may be true, except that its *intent* is that it shall be derived from. D can not derive a class from a struct, so the base-with-no-virtuals needs to be expressed as a class in D for the arrangement to translate and interact with D code.
Comment #1 by gooberman — 2018-05-09T21:33:04Z
See: The giant workaround I had to do for Binderoo for this very thing in my DConf 2017 talk.
I believe the reason classes always have a vtable is so safe casting is still guaranteed. But that's just an assumption.
Comment #2 by turkeyman — 2018-05-09T21:53:56Z
What does "safe casting" mean?
You mean that it might require pointer adjustment?
I mean, it's absolutely necessary that when casting extern(C++) classes, that some special logic is applied which may need to perform a pointer adjustment, just like when casting C++ classes in C++ ;)
We can't escape that. We either need to support it, or it's just broken.
At very least, we should emit an error when an extern(C++) class is declared with no virtual members saying it's not supported.
Comment #3 by pro.mathias.lang — 2019-06-14T06:54:46Z
*** Issue 18449 has been marked as a duplicate of this issue. ***
Comment #4 by robert.schadek — 2024-12-13T18:58:43Z