enum to flags

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 29 02:18:50 PDT 2015


On Tuesday, 29 September 2015 at 03:31:44 UTC, Nicholas Wilson 
wrote:
> so I have a bunch of enums (0 .. n) that i also want to 
> represent as flags ( 1 << n foreach n ). Is there anyway to do 
> this other than a string mixin?
>
> use like:
>
> enum blah
> {
>     foo,
>     bar,
>     baz,
> }
>
> alias blahFlags = EnumToFlags!blah;
>
> static assert(blahFlags.baz == 1 << blah.baz)

Answering a slightly different question, I just wanted to be sure 
you're aware of this old idiom:

enum blah
{
     foo = 0b1;
     bar = 0b10;
     baz = 0b100;
     //and so on...
}

auto fdsa = blah.foo | blah.baz;
assert(fdsa & blah.foo);
assert(fdsa & blah.baz);
assert(!(fdsa & blah.bar));


More information about the Digitalmars-d-learn mailing list