Manifest constants (was const again)

Walter Bright newshound1 at digitalmars.com
Fri Dec 7 02:37:36 PST 2007


Don Clugston wrote:
> I think they are only used that way because there isn't an alternative.
> Note that it reduces all kinds of potential for error checking.
> For example, the Windows API contains loads of #defines which are 
> actually enums. But it also contains loads of #defines which are bit masks.
> A true enum is a genuine type. Bitwise operations without a cast are 
> always a bug, and even arithmetic operations probably are too. eg
> enum { RED, BLUE, GREEN }
> (BLUE | RED) is nonsense (since if RED=0, BLUE=1, (BLUE|RED)==BLUE).

I see that as one use for enums, but another use is for grouping 
together logically related constants. A set of bit masks would fit in 
the latter category.

> Although
> (a) I don't know how often such bugs occur in practice; and
> (b) conceivably, you could generate a warning whenever such an operation 
> was performed on a enum which contained at least one member which did 
> not have an explicitly assigned value.
> 
> I think there's significant merit in distinguishing genuine enums from 
> compile-time constants.



More information about the Digitalmars-d mailing list