TITLE: stepping through enum values (Newsgroups: comp.lang.c++.moderated, 15 Nov 96) ROHAN: rohan@Silvaco.COM (Michael Rohan) >I'm try to do something I think should be very simple and direct: loop >thru all the values defined in an enum, e.g., > enum A { x, y, z }; CLAMAGE: clamage@taumet.Eng.Sun.COM (Steve Clamage) There is no direct support in the language for stepping through enum values, because the enumerators need not have any particular relationship to one another. For example, consider: enum B { w=10, x=-1, y=12, z=10 }; What should it mean to loop through the enumerators? What should be the result of ++ or -- applied to a value of type B? One can imagine plausible rules, but C and C++ leave the area open. If you choose to use a coding discipline as in your example, you can add dummy 'first' and 'last' values and define your own ++ operator. Example: enum A { A_first=0, x, y, z, A_last=z }; A& operator++(A& a) { // prefix ++ if( a >= A_last ) throw ...; // some exception a = (A)((long)a + 1); return a; } ... for( A a=A_first; a