[comp.lang.c++] Conversion between enum and int

horstman@sjsumcs.sjsu.edu (Cay Horstmann) (12/18/89)

I just got around to firing up my Zortech compiler version 2.0. It flags
as an *error* the following:

enum Bool { FALSE, TRUE };
// ...
Bool x;
// ...
x = !x;


Is this really an error? It was my impression that conversions from enum
to int and from int to enum are ok, i.e. the above should correctly work
to
	(1) Convert x to an int
	(2) ! that int
	(3) Convert the result back to enum Bool and stuff into x

I know that conversions *between* two different enums aren't ok without a cast.

I sure hope I am right and Walter isn't--I *like* enum Bool { FALSE, TRUE };
and it would be pretty useless if one couldn't do && || ! on it. Hey, if I
wanted fascism in enumerated types, I'd program in Ada :-)

Cay

bright@Data-IO.COM (Walter Bright) (12/19/89)

In article <1989Dec18.034022.19303@sjsumcs.sjsu.edu> horstman@sjsumcs.SJSU.EDU (Cay Horstmann) writes:
<I just got around to firing up my Zortech compiler version 2.0. It flags
<as an *error* the following:
<enum Bool { FALSE, TRUE };
<Bool x;
<x = !x;
<	(1) Convert x to an int
<	(2) ! that int
<	(3) Convert the result back to enum Bool and stuff into x
Implicit conversions from int to enums are not allowed. Try:
	x = (Bool) !x;

stt@inmet.inmet.com (12/20/89)

Presumably, you can get x = !x to work if you implement
your own operator! for Bool (and &&, || -- are those possible?).

horstman@sjsumcs.sjsu.edu (Cay Horstmann) (12/23/89)

In article <21000006@inmet> stt@inmet.inmet.com writes:
>
>Presumably, you can get x = !x to work if you implement
>your own operator! for Bool (and &&, || -- are those possible?).

(This was in response to my query whether indeed a type conversion
from int to enum Bool { FALSE, TRUE }; required a cast.)

NO! You cannot attach overloaded operators to an enum, only to a class.
I must say that I have been somewhat unwilling to go as far as to
define
	class Bool { int truthValue; Bool operator!(); /* ... */ };
There are limits to everything. 

In fact, I am going back to good old
	#define FALSE 0
	#define TRUE 1
	#define Bool int

Cay