Thread overview
get vtable size
May 07, 2017
Mike B Johnson
May 08, 2017
Jacob Carlborg
May 08, 2017
Basile B.
May 07, 2017
how many elements(virtual functions) are in the __vptr?
May 08, 2017
On 2017-05-07 06:01, Mike B Johnson wrote:
> how many elements(virtual functions) are in the __vptr?

I guess you can use __traits(allMembers) and __traits(isVirtualMethod) [1].

[1] http://dlang.org/spec/traits.html

-- 
/Jacob Carlborg
May 08, 2017
On Sunday, 7 May 2017 at 04:01:43 UTC, Mike B Johnson wrote:
> how many elements(virtual functions) are in the __vptr?

You don't need the size. The index you get with __traits(virtualIndex) is always valid. (https://dlang.org/spec/traits.html#getVirtualIndex)

However as an exercise you can still count them:

- iterate all the member
- check each overload with __traits(isVirtualMethod)

---
template virtualMethodCount(T)
{
    size_t c()
    {
        size_t result;
        foreach (member; __traits(allMembers, T))
            static if (is(typeof(__traits(getMember, T, member))))
            {
                foreach (overload; __traits(getOverloads, T , member))
                    result += __traits(isVirtualMethod, overload);
            }
        return result;
    }
    enum objectVmc = 4; // toHash, opCmp, opEquals, toString
    enum virtualMethodCount = c() - objectVmc;
}

unittest
{
    class Foo{}
    static assert( virtualMethodCount!Foo == 0);
    class Bar{void foo(){} int foo(){return 0;}}
    static assert( virtualMethodCount!Bar == 2);
    class Baz{private void foo(){} int foo(){return 0;}}
    static assert( virtualMethodCount!Baz == 1);
}
---