The attribute __vptr of object instances can be useful in some special situations. To compare the __vptr of an object instance to the __vptr of a class you first need to find the __vptr of a class. You can do it with code similar to: class Foo { __gshared static immutable(immutable(void)*) __vptr; static this() { scope auto aux = new typeof(this); __vptr = aux.__vptr; } //... } void main() {} But this is not handy (even if a class mixin can help). So a built-in __vptr attribute can be added to classes too. Possible disadvantages: the need for __vptr is rare. ------------- Note: there are other ways to implement virtual functions, like using a dispatch tree, so the D docs can say that __vptr can be absent in D compilers that don't use a virtual table or in other situations.
Currently this seems the most efficient way to have a static class attribute that contains a __vptr (here converted to a size_t and named classId): class Foo { static immutable size_t classId; static this() { classId = *(cast(size_t*)(typeid(Foo).init.ptr)); } } void main() { assert(cast(size_t)((new Foo).__vptr) == Foo.classId); } With it DMD 2.049 generates a very light static constructor: _D4test3Foo12_staticCtor1FZv comdat mov EAX,_D4test3Foo7__ClassZ[0Ch] mov ECX,[EAX] mov _D4test3Foo7classIdyk,ECX ret
Could be taken from typeinfo, see object.d: class TypeInfo_Class : TypeInfo { ... void*[] vtbl; /// virtual function pointer table Interface[] interfaces; /// interfaces this class implements ... } So I believe those who truly need it will find it. And solution provided is actually fine for such low-level goal.