Manifest constants (was const again)

Lars Ivar Igesund larsivar at igesund.net
Fri Dec 7 01:13:41 PST 2007


Don Clugston wrote:

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

I agree. Of the keywords mentioned (macro, final, alias and enum), enum is
the _worst_ match for this feature.

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango



More information about the Digitalmars-d mailing list