| |
| Posted by Basile B. in reply to Mike B Johnson | PermalinkReply |
|
Basile B.
Posted in reply to Mike B Johnson
| 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);
}
---
|