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

Marc jckj33 at gmail.com
Thu Nov 23 01:03:02 UTC 2017


On Thursday, 23 November 2017 at 01:01:42 UTC, Michael V. 
Franklin wrote:
> 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

This was fast! Thanks


More information about the Digitalmars-d-learn mailing list