I
Ian Collins
You could if enumerated types and int where different types withoutKeith said:Ian Collins said:Thad Smith wrote:
[snip]
Attempts to assign anything other than an enumeration constant defined
for the associated enumeration type or another enumeration variable of
the same type would be a useful compiler or lint warning. Refusing to
translate in such cases would make the compiler non-conforming.
Maybe this is a topic for comp.std.c, I still think the C standard is
broken in this instance.
I wouldn't say it's broken. The design of enumerated types isn't
ideal, but I don't think it's possible to fix it (though it would be
possible to invent a new feature that doesn't have the drawbacks of
the current types).
For example:
enum foo { a, b };
enum foo e;
int i = 2;
e = i;
In this case, obj is an object of type "enum foo", and the value of i
is assigned to it. The value being assigned doesn't match any of the
enumerators of the type, but there's no way in general to detect this
at compilation time.
automatic conversion, which is the way C++ does it, so you simply can't
assign an int to an enum.
Again, if the only legal things you can assign to e are foo namedIf it's going to be detected at run time, you need to introduce an
entire mechanism for detecting run time errors, something that C
doesn't currently have. And for this particular check, a simple range
check is insufficient; consider replacing the type declaration above
with:
enum foo { a = 1, b = 37 };
constants, the error can be flagged at compiler time.
Simply remove the automatic conversion from an integer type to anC's enumerated types really don't provide much more than a set of
named constants. There are other languages that have more type-safe
enumerated types (Pascal and Ada, for example), but I don't think it's
likely that C's enumerated types can be significantly improved without
breaking existing code (or the language itself).
If you can come up with a proposal, though, by all means go for it.
enumerated type. Automatic conversion form enum to integer types should
be permitted because enumerated types are constrained to a set of
integer values. Thus assignment from enum to int doesn't violate any
constraints.