Can I count the of enum's fields at compile time?

Michael V. Franklin slavo5150 at yahoo.com
Thu Nov 23 01:01:42 UTC 2017


On Thursday, 23 November 2017 at 00:58:21 UTC, Marc wrote:
> for example:
>
> enum A { a = -10, b = -11, c = -12, d = -13, e = -34}
>
> enum int countOfA = coutOfFields(A); // 5 fields

https://dlang.org/spec/traits.html#allMembers

enum A { a = -10, b = -11, c = -12, d = -13, e = -34}

enum int countOfA = __traits(allMembers, A).length; // 5 fields

static assert(countOfA == 5);

Mike


More information about the Digitalmars-d-learn mailing list