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?

Was it helpful?

Solution

A few things:

  1. In C++, a class that has no virtual functions will have zero overhead.
  2. 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.
  3. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top