Range of enum type values
j at j.nl
Fri Dec 27 12:14:47 UTC 2019
I am wondering about the valid range of values of an enum type.
I couldn't find anything explicit about this in the language
Consider this code:
A = 1,
B = 2,
C = 4
bool rangeCheck(Flags f)
return (f >= Flags.min) && (f <= Flags.max);
bool preciseCheck(Flags f)
return (f == Flags.A) || (f == Flags.B) || (f == Flags.C);
Is `rangeCheck` guaranteed to return true? Is `preciseCheck`
guaranteed to return true?
A variable of type Flags is always initialized to Flags.A.
Integer assignment is not allowed.
So `Flags f` should always have a value of A B or C, right?
No. This code is accepted:
return Flags.A | Flags.B; // and so is `^`, `&`, `+`, `*`, ...
I'd like to have the value range implications of the use of
operators on enum values explicitly mentioned in the spec.
Current compiler behavior results in an infinite value range.
(but it's implicit behavior, i.e. not explicitly mentioned in
- Currently, are operations resulting in a value larger than the
underlying integer storage type UB, like for normal signed
- Should we limit the range of valid values of the Flags enum
(C++ defines valid range to be [0..7])?
- Do we want to limit operations allowed on enum types? Or change
the result type? (e.g. the type of `Flags + Flags` is `int`
instead of `Flags`.
More information about the Digitalmars-d