So I was just reading http://stackoverflow.com/questions/1448396/how-to-use-enums-as-flags-in-c

And did a quick test:

enum STC
{
        A = 0x1,
        B = 0x2,
        C = 0x4
}
enum FOO
{
        F = 0x8,
        G = 0x10
}

void main()
{
        STC s = STC.A | STC.C;
        STC s2 = s & STC.A;
        auto s2_2 = s & STC.A;
        static assert(is(typeof(s2_2) == STC));

static assert(!__traits(compiles, { if (s & FOO.F) {} })); // fails. that one's hard to track down static assert(!__traits(compiles, { auto s3 = s & FOO.F; })); // fails, cause implicitly converts to int
        static assert(!__traits(compiles, { STC s4 = s & FOO.F; }));
        static assert(!__traits(compiles, { FOO s5 = s & FOO.F; }));

        static assert(!__traits(compiles, { STC t = STC.A | FOO.F; }));
        static assert(!__traits(compiles, { auto t = STC.A | FOO.F; })); // 
fails

static assert(!__traits(compiles, { if (s & STC.B == 0) {} })); // works, but error not gagged
        static assert(!__traits(compiles, { if (s && STC.B) {} }));      // 
fails
}

Does it really make sense to allow bitwise operations on different enums?
There should at least be some way to get this straight without having to resort to a heap of code like in C++: http://www.artima.com/cppsource/safelabels.html

Reply via email to