get vtable size
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 8 06:01:22 PDT 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);
}
---
More information about the Digitalmars-d-learn
mailing list