[comp.lang.c++] enums

grunwald@uiucdcsm.cs.uiuc.edu (02/13/88)

I want to have an ostream printer for enums, but I've got the feeling that
enums are just ints internall.

If I say
---
enum foo {
	bar,
	baz
};

ostream& operator<<(ostream&, foo)
---

I get an error stating that << is defined in two ways, which I think is
related to there being an << operator for int.

So, anyway to have per-enum printers?

dirk grunwald
univ. of illinois
grunwald@m.cs.uiuc.edu

johnc@plx.UUCP (John C.) (01/12/90)

Having used full-featured enums (and subranges -- *sigh* ) 
in other languages, this debate about "should you be prevented from 
assigning an int to an enum without a cast" seems like needless
hand-wringing.  IMHO, *of course* you shouldn't be allowed to do this 
without a cast (this is to offend some diehard original-C types I know).  

But if I have (pardon the loose syntax):

  enum Fruit { Apple, Orange, Banana, Lemon }
  enum Vegetable { Tomato [<==debatable ;-)], Squash, Rutabaga }
  ...
  Fruit fruit;
  Vegetable veg;

then the assignment 'fruit = 1' is as much an error as 
'fruit = Squash' or 'fruit = veg'.

By 'full-featured' enums, I meant (for one thing) the ability 
(in other languages) to declare an array with an 'enum' subscript:
  
  int fruitPrice[ Fruit ];

This is so simple but so powerful.  In the "C++ Primer" I didn't
see a way to do this, probably because it presupposes arrays with
non-zero base subscripts, which I'm pretty sure C++ also lacks.
Both features are conceptually simple but greatly improve readability 
and robustness.  Ditto subrange types.

...My three wishes for today.  (Are the C++ architects listening?)

/John Ciccarelli, Plexus Software (johnc@plx.uucp || ...sun!plx!johnc)
[opinions mine]

sakkinen@tukki.jyu.fi (Markku Sakkinen) (01/15/90)

In article <2205@plx.UUCP> johnc@plx.UUCP (John Ciccarelli) writes:
>Having used full-featured enums (and subranges -- *sigh* ) 
>in other languages, this debate about "should you be prevented from 
>assigning an int to an enum without a cast" seems like needless
>hand-wringing.  IMHO, *of course* you shouldn't be allowed to do this 
>without a cast (this is to offend some diehard original-C types I know).  

Seconded. In his "The C Book : Featuring the draft ANSI C Standard"
(Addison-Wesley 1988), Mike Banahan introduces ANSI enum types approximately:
"These belong to the category of half-baked." At the end of the section,
I think he asks any reader who invents a good application for this kind
of enumerations to inform him too. (The book is recommended especially
to people who like the wry British kind of humour.)

> [...]
>
>By 'full-featured' enums, I meant (for one thing) the ability 
>(in other languages) to declare an array with an 'enum' subscript:
>  
>  int fruitPrice[ Fruit ];
>
>This is so simple but so powerful.  In the "C++ Primer" I didn't
>see a way to do this, probably because it presupposes arrays with
>non-zero base subscripts, which I'm pretty sure C++ also lacks.
>Both features are conceptually simple but greatly improve readability 
>and robustness.  Ditto subrange types.

Perhaps you should look around for other OO languages than C++ ...
Readability and robustness aren't important goals in the C world.
By the way, Niklaus Wirth himself has eradicated enumerations,
subranges, and several other things from his newest language Oberon,
the successor of Pascal and Modula-2, which is mildly object-oriented.
I think he has overdone it.

Markku Sakkinen
Department of Computer Science
University of Jyvaskyla (a's with umlauts)
Seminaarinkatu 15
SF-40100 Jyvaskyla (umlauts again)
Finland