Manifest constants (was const again)

Don Clugston dac at nospam.com.au
Fri Dec 7 00:21:59 PST 2007


Walter Bright wrote:
> mandel wrote:
>> I think enums shouldn't be misused for
>> compile time constants.
> 
> But they already are used that way.

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).

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