Are enums safe to use in portable code?

Erik Naggum enag at ifi.uio.no
Fri Feb 1 10:37:15 AEST 1991


In article <1991Jan30.210255.16804 at csrd.uiuc.edu>, Brian Bliss writes:

    The alliant fx C compiler (for an fx1 or fx8, not fx2800), supports
    them, but gives errors when you try to compare two enum constants.

    bb

Assume these definitions:

	enum { frotz, klutz } foo;					[1]
	enum { gunk, junk } bar;					[2]

Which of these are your compiler complaining against?

	frotz == klutz							[3]
	frotz == gunk							[4]
	frotz < klutz							[5]
	frotz < junk							[6]

The declaration in [1] declares a distinct (anonymous) type of which
foo is an object.  Likewise [2] declares another distinct (anonymous)
type of which bare is an object.  frotz and klutz [1] are names of
constant values of the type of object of which foo is an instance.
Likewise for gunk and junk [2].

This makes the comparison in [3] valid (but slightly pointless), since
we're comparing values of the same type, while the latter does not.

In [5] and [6], "<" could be any of "<", "<=", ">", and ">=".  I'm not
sure about the welldefinedness of these comparisons, since I have seen
compilers barf on expressions such as

	foo < klutz							[7]

since it implicitly entails a conversion to int, which nonetheless is
the base type for enums.  Careful consulting of the ANSI C standard
may be needed to verify this.

It would be helpful if you gave examples of the code on which your
compilers gags, as well as the precise error message produced.

--
[Erik Naggum]	Snail: Naggum Software / BOX 1570 VIKA / 0118 OSLO / NORWAY
		Mail: <erik at naggum.uu.no>, <enag at ifi.uio.no>
My opinions.	Wail: +47-2-836-863	Another int'l standards dude.



More information about the Comp.lang.c mailing list