www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get vtable size

reply Mike B Johnson <Mikey Ikes.com> writes:
how many elements(virtual functions) are in the __vptr?
May 06 2017
next sibling parent Jacob Carlborg <doob me.com> writes:
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
prev sibling parent Basile B. <b2.temp gmx.com> writes:
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); } ---
May 08 2017