> On Wed, 25 Jan 2012 03:22:03 +0100, Trass3r <u...@known.com> > wrote: > > Does it really make sense to allow bitwise operations > on different enums? > > Maybe. Certainly sometimes, but those could just as easily > use casts. > > > There should at least be some way to get this straight > without having to > > resort to a heap of code like in C++:
To my understanding, converting an int to an enum (In C++ or D) is basically illegal. You'd have to do force casting to get it to work, which isn't safe. Enums seem better suited for a long range of codes that don't have specific bits for on/off If it hasn't been suggested yet, I am currently using a block of personal code to use enums as flags. I'll give you a stripped down version of the structure, if everyone says 'yay' or perhaps Walter feels he wants it, we can propose maybe get it in phobos. How it works, is internally it will store the flag as an int (or whatever type S is), then you can do checks and it will convert the enum to an int and do appropriate bit casts. Haven't tried to consider how to get a array of separate flags. ///T of type ENUM, and S of an integral. struct HandleFlags(T, S) { S state; ///Holds state. alias T T_Enum; this(T[] setFlags...); ///Returns true/false if a specific ENUM flag has been set. bool check(T flag); /** Checks if a flag has been set, returning that ENUM, otherwise returning the Else flag. */ T checkElse(T flag, T Else); ///Sets specific flag on or off. Default sets flag void setFlag(T flag, bool on_off = true); ///reverses the state of a specific flag. void flipFlag(T flag); } Usage is like this. enum ETEST {zero = 0, one = 1, two = 2, four = 4} HandleFlags!(ETEST, int) ftest; HandleFlags!(ETEST, int) ftest1(ETEST.one); //1 HandleFlags!(ETEST, int) ftest2(ETEST.two, ETEST.four); //6