Re: Variable arguments of enum type



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



Relevant Pages

  • Re: enum type int or unsigned int?
    ... that have type int and may appear wherever such are permitted. ... values of all the members of the enumeration. ... "enum" types are declared via the following syntax: ... so the loop will continue to run. ...
    (comp.lang.c)
  • Re: enum type int or unsigned int?
    ... that have type int and may appear wherever such are permitted. ... values of all the members of the enumeration. ... "enum" types are declared via the following syntax: ... so the loop will continue to run. ...
    (comp.lang.c)
  • Re: Behaviour of enumerated types (was: Re: Immutable instances, constant values)
    ... >> My notion of enum comes from Pascal ... I will look in PyPI. ... Does your concept of enumeration not have a fixed order of a set of names? ... >> So I'd say enums should also be int subtypes... ...
    (comp.lang.python)
  • Re: Custom Attributes, Shared methods, Derived classes and reflection
    ... description associated with an enum value. ... enumeration value will give you the name as defined by the enumeration, ... Dim fi As FieldInfo = value.GetType.GetField ... Public Shared Function GetEnumValue ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Custom Attributes, Shared methods, Derived classes and reflection
    ... description associated with an enum value. ... enumeration value will give you the name as defined by the enumeration, ... Dim fi As FieldInfo = value.GetType.GetField ... Public Shared Function GetEnumValue ...
    (microsoft.public.dotnet.languages.vb)