enum to flags

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 29 06:44:13 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)

Take a look at the BitFlags template as well. It won't create 
such an enum for you, but will provide a convenient wrapper for 
using it afterword:

http://dlang.org/phobos/std_typecons.html#.BitFlags


More information about the Digitalmars-d-learn mailing list