Re: Variable arguments of enum type
- From: Harald van Dijk <truedfx@xxxxxxxxx>
- Date: Tue, 01 Apr 2008 22:35:27 +0200
On Tue, 01 Apr 2008 20:18:10 +0000, Walter Roberson wrote:
In article <b603a$47f291a5$541dfcd3$5001@xxxxxxxxxxxxxxxxxxxxxxxx>,
Harald van =?UTF-8?b?RMSzaw==?= <truedfx@xxxxxxxxx> wrote:
int main(void)
{
enum EnumerationType variable = Value1; VarArgFunc(0, variable);
}
If you were to use
VarArgFunc(0, (unsigned int)variable);
then because the value of each identifier is of type int, you know that
if your variable 'variable' was set to one of the enumeration
identifiers that the cast will have a predictable result no matter what
the implementation type used for the enumeration type as a whole.
I used (unsigned int) instead of (int) against the off chance that the
implementation choose an unsigned integral type for the enumeration
type, in which case you could potentially run into issues with (int) if
the enumeration value currently exceeded INT_MAX.
Luckily, I don't have to worry too much about that, there are plenty of
assert(enum_value >= min_enum_value && enum_value <= max_enum_value);
I can't think of why
an implementation would think it a good idea to use an unsigned integral
type to hold values that are allowed to be both negative and positive
(it'd need a lot of fixups to work right!) but the C89 verbage doesn't
prohibit it. I would imagine that -in practice-, using (int) would be
fine (and much easier to program with). The C89 verbage is probably
there to allow char or short to be used if the declared constants fit.
This I don't understand. Different enumeration types can have different
sizes. When a compiler sees
enum EnumType1 {
Value1 = 1, Value2 = 2
};
it knows that there aren't any negative values to worry about. When that
compiler later sees
enum EnumType2 {
Value1 = 1, Value2 = -1
};
enum EnumType2 can be distinct from enum EnumType1; there's no
requirement that all enumeration types are compatible. So why does the
compiler have to worry about enum EnumType1 holding negative values?
Note that an enumeration type is not restricted to holding one of the
enumerated values -- anything between the minimum and maximum of them is
valid to store in the enumeration variable (but anything outside that
range might or might not fit.)
Mostly true, and I've run into that already, with the assumption of real
code that 0x100 is a valid value for an enum which turned out to have a
range of 0x00 to 0xFF (unsigned char).
I say mostly true because
enum {
Value1 = 1,
Value2 = 2 };
must be capable of storing 3. The enum must be an integer type with at
least two value bits, and an integer type with at least two value bits
can _always_ store the value 3.
.
- Follow-Ups:
- Re: Variable arguments of enum type
- From: CBFalconer
- Re: Variable arguments of enum type
- References:
- Variable arguments of enum type
- From: Harald van Dijk
- Re: Variable arguments of enum type
- From: Walter Roberson
- Variable arguments of enum type
- Prev by Date: Re: Variable arguments of enum type
- Next by Date: Re: Variable arguments of enum type
- Previous by thread: Re: Variable arguments of enum type
- Next by thread: Re: Variable arguments of enum type
- Index(es):
Relevant Pages
|