On 2014-04-07 6:00 AM, Karl Tomlinson wrote:
chiaki ISHIKAWA writes:
I think 7.2 10 is also relevant here.
--- quote ---
An expression of arithmetic or enumeration type can be converted
to an enumeration type explicitly. The
value is unchanged if it is in the range of enumeration values of
the enumeration type; otherwise the resulting
enumeration value is unspecified.
--- end quote
I take so :
typedef enum { a = 1, b, c = 10 } T;
T x;
Anyway, in my example above, a compiler can do anything
if x = 32 is executed (?).
Note here the "enumeration value" is "unspecified", which I assume
merely means the compiler can choose anything for the value of x.
That might be considerably safer than "undefined behavior" of the
"program".
Right. The intention here is that the compiler is allowed to pick some
underlying integer type, which must be able to represent all declared
values of the enumeration, and then silently accept in-range values and
truncate out-of-range values *for that type* -- but it's *not* allowed
to apply "assume the programmer never does that" optimizations as it
would for undefined behavior.
I don't know what the C++ committee's attitude was, but the C committee
specifically wanted to allow use of `enum` to declare bitmasks:
enum X {
X_FOO = 0x0001,
X_BAR = 0x0002,
/* ... exhaustive list of bit flags here ... */
X_BLURF = 0x8000
};
For that use case, any _combination_ of X_* flags must be representable
by the underlying integer type; note that many such combinations are
outside the *declared* range of values.
zw
_______________________________________________
dev-platform mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-platform