www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3939] New: Built-in __vptr attribute for classes too

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3939

           Summary: Built-in __vptr attribute for classes too
           Product: D
           Version: 2.041
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-03-12 13:33:25 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 12 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3939



--- Comment #1 from bearophile_hugs eml.cc 2010-10-24 18:27:12 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 24 2010