Enum template for cpp binding

Paul Backus snarwin at gmail.com
Mon Feb 15 14:03:26 UTC 2021


On Monday, 15 February 2021 at 12:37:14 UTC, novice3 wrote:
> This is reduced example.
> I am sorry for this type of question,
> but could please anybody show me template for this boring 
> coding?
> Is this possible to avoid this manual coding?
> Show me direction or examples please.

This will do most of it:

     mixin template declareAnonymous(E, string sep = "_")
         if (is(E == enum))
     {
         static foreach (memberName; __traits(allMembers, E))
         {
             mixin(
                 "alias ",
                 __traits(identifier, E), sep, memberName,
                 " = __traits(getMember, E, memberName);"
             );
         }
     }

Usage:

     enum MY_ENUM { A, B, C }

     mixin declareAnonymous!MY_ENUM;

     static assert(MY_ENUM_A == MY_ENUM.A);
     static assert(MY_ENUM_B == MY_ENUM.B);
     static assert(MY_ENUM_C == MY_ENUM.C);


More information about the Digitalmars-d-learn mailing list