While we're on topic, the size of a class type and a class variable both are constant on a platform, e.g. 8 bytes on 64 bit systems. To get the size of actual instances (objects) of this type, one needs to use the classInstanceSize trait:

class C {
  int i;
}

void main() {
  auto a = new C();
  static assert (a.sizeof == C.sizeof);

  pragma(msg, "Size of the reference: ", a.sizeof);
  pragma(msg, "Size of the object: ", __traits(classInstanceSize, C));
}

Prints the following during compilation my system:

Size of the reference: 8LU
Size of the object: 20LU

20 is the sum of two hidden variables (the vtbl pointer and the monitor) and the member 'i'.

As I learned recently but never used yet, extern(C++) classes do not have the monitor member. So, the instances of the following type would be 12 on a 64 bit system:

extern (C++)
class C {
  int i;
}

Ali

Reply via email to