enum to flags
Cauterite via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Sep 28 23:08:01 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?
You could cheat with operator overloading:
enum blah {
foo,
bar,
baz,
};
struct EnumToFlags(alias E) {
template opDispatch(string Name) {
enum opDispatch = 1 << __traits(getMember, E, Name);
};
};
alias blahFlags = EnumToFlags!blah;
static assert(blahFlags.foo == (1 << blah.foo));
static assert(blahFlags.bar == (1 << blah.bar));
static assert(blahFlags.baz == (1 << blah.baz));
More information about the Digitalmars-d-learn
mailing list