nagle@well.sf.ca.us (John Nagle) (03/17/91)
Is the operator "++" permitted on enumeration types in C++?
Zortech C++ 2.1 rejects it as a request for an invalid type conversion.
Zortech C 2.1 is quite happy with it, leading me to suspect this is
policy, not a bug. But Strostrup's book is silent on the subject.
Actually, increment and decrement on enumeration types make more
sense than allowing addition of two values of the same enumeration
type. Consider
enum color_t {red,blue,green,yellow};
color_t lamp1, lamp2;
int i;
lamp1 = red; // OK
lamp1++; // reasonable
lamp1 = red + green; // silly,hould be an error
lamp1 = red + 1; // better
i = lamp1 - lamp2; // difference is logically an integer
But this is probably too radical for C++.
John Naglecok@islsun.Kodak.COM (David Cok) (03/18/91)
In article <23651@well.sf.ca.us> nagle@well.sf.ca.us (John Nagle) writes: > > Is the operator "++" permitted on enumeration types in C++? >Zortech C++ 2.1 rejects it as a request for an invalid type conversion. >Zortech C 2.1 is quite happy with it, leading me to suspect this is >policy, not a bug. But Strostrup's book is silent on the subject. > > Actually, increment and decrement on enumeration types make more >sense than allowing addition of two values of the same enumeration >type. Consider > > enum color_t {red,blue,green,yellow}; > color_t lamp1, lamp2; > int i; > > lamp1 = red; // OK > lamp1++; // reasonable > lamp1 = red + green; // silly,hould be an error > lamp1 = red + 1; // better > i = lamp1 - lamp2; // difference is logically an integer > >But this is probably too radical for C++. > > > > > > > John Nagle See the ARM section 7.2 in which it is explicitly stated that the results of numeric computations cannot be assigned (even via ++) to a variable of an enumerated type. The basic reason is that the integer representation of the enumerated values may consist of something other than consecutive integers. David R. Cok Eastman Kodak Company cok@Kodak.COM
ark@alice.att.com (Andrew Koenig) (03/19/91)
In article <23651@well.sf.ca.us> nagle@well.sf.ca.us (John Nagle) writes: > Is the operator "++" permitted on enumeration types in C++? > Zortech C++ 2.1 rejects it as a request for an invalid type conversion. > Zortech C 2.1 is quite happy with it, leading me to suspect this is > policy, not a bug. But Strostrup's book is silent on the subject. Not quite. In effect, ++x is equivalent to (x=x+1), with all the conversion rules that that implies. Now, if x is of an enumeration type, x+1 is an int, and there is no conversion from int to enum without an explicit cast. Therefore ++ cannot apply to an object of enumeration type. -- --Andrew Koenig ark@europa.att.com
jimad@microsoft.UUCP (Jim ADCOCK) (03/26/91)
In article <23651@well.sf.ca.us> nagle@well.sf.ca.us (John Nagle) writes: > Is the operator "++" permitted on enumeration types in C++? > Zortech C++ 2.1 rejects it as a request for an invalid type conversion. > Zortech C 2.1 is quite happy with it, leading me to suspect this is > policy, not a bug. But Strostrup's book is silent on the subject. By "Stroustrup's book" you seem to mean "The C++ Programming Language" not Stroustrup's later and more detailed and more up-to-date "The Annotated C++ Reference Manual." I suggest you get the later. Page 54 states: "The increment and decrement operators are not defined for enumerations...."