Question
I thought I heard that D classes have two words (2 void*) of overhead instead of the one word required by C++. I also heard that the vtable layout is incompatible with C++. Do I remember correctly? If so what is the rationale for these decisions?
Solution
A few things:
- In C++, a class that has no virtual functions will have zero overhead.
- In D, a class always inherits virtual functions from
Object
, so it always has that__vptr
overhead, but also has a__monitor
, which C++ class objects don't have. - In both C++ and D, there will be additional
vptr
for each interface that the class implements.
The vtable layout is incompatible with C++ because D includes a pointer to a TypeInfo
instance, which has runtime type information about the class. C++ obviously doesn't have that, so it is incompatible.
OTHER TIPS
The extra pointer is to a monitor object for synchronized classes (there have apparently been talks about removing it for unsynchronized classes but that hasn't happened). The layout is documented in the ABI section of the spec.
I'm not sure how the vtable layout differs from how C++ compilers do it. Someone else can probably answer that.