[comp.lang.c] enum type checking and switches question

shino@qip.UUCP (Rei Shinozuka) (10/26/89)

I am new to this newsgroup, so apologies if the following
posting is innapropriate/naive.

Why is it that cc does not perform enum type checking
for switches?  For assignments and comparisons, the compiler
makes sure that the enum types match, as in the if statement
below.

  enum bar {
    ONE,
    TWO
  } foo;
  int baz;

  if (foo == ONE)   /* OK both of type enum bar */ 
    ...
  if (baz == ONE)   /* warning incompatible types */
    ...

Given the same declarations above, the compiler doesn't seem to care
about the enum types of the switch expression and constant expression.  

  switch(foo){
    case ONE:    /* OK both or type enum bar */
      ...
  }/*end switch*/
  switch(baz){
    case ONE:    /* no complaints, though types differ */
      ...
  }/*end switch*/

It seems to me that the compiler has enough information to flag
a type incompatibility as it does in the first example with the
second if.  Is there a reason the compiler doesn't do this
in the case (bad pun) of switches?

thanks,

-rei

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (10/26/89)

  You have to know the history of enum to understand why it works as
poorly as it does. Someone was out of space for #defined values in their
preprocessor, and so added enum to get more space in a diferent symbol
table. That's why you can use the name of an enum value as if it was an
ordanary int, because it was intended to work that way.

  This was discussed at one of the early meetings of X3J11 (I was on for
just over two years) and because of "existing practice" it was agreed
that adding a real enum type with range checking, etc, was not
practical. enum can add clarity to reading the code, but it missed its
chance to allow the compiler to do more runtime checking.

  Makinging this work in a range checking manner would break a lot of
programs, but I wish there was a way. Your compiler is not broken for
failing to perform the checks, that's the way it works.

-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

tneff@bfmny0.UU.NET (Tom Neff) (10/26/89)

In article <1489@crdos1.crd.ge.COM> davidsen@crdos1.UUCP (bill davidsen) writes:
>  Makinging [enum] work in a range checking manner would break a lot of
>programs, but I wish there was a way. Your compiler is not broken for
>failing to perform the checks, that's the way it works.

Lint, on the other hand, ought to be more than welcome to perform the
appropriate checks.  I think Gimpel caught me once on enum, but I dunno
whether or what other implementations do.
-- 
Hey, where'd the Colombian Coffee ads go all of   %8  Tom Neff
a sudden!  Is Juan Valdez hiding in Panama?       8%  tneff@bfmny0.UU.NET