With not working with BitFlags

Dukc via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 20 01:12:04 PST 2017


This is fairly complex thing, but I managed to get it working:

template EnumToFlags(E) if(is(E == enum))
{
     import std.traits, std.typecons, std.string;

     private static auto implementation()
     {
         string result;
         foreach(i, enumMem; EnumMembers!E)
         {
             result ~= format("enum %s = 1 << %s;\n", enumMem, i);
         }
         return result;
     }

     mixin(implementation);
};

enum X
{
     a,b,c
}

void main()
{
     alias q = EnumToFlags!X;

     with(q)
     {
        auto m = a;
     }
}

It may well be that with(x) only looks for x members, it probably 
does not try to look for templates. If so, opDispatch does not do 
the trick here. But the above does.


More information about the Digitalmars-d-learn mailing list