views:

123

answers:

3

I copy and pasted some code that increments an enum:

myenum++;  

This code worked fine as it was compiled in VS.NET C++ 2003

I am now developing in VS 6.0 and get the error:

error C2676: binary '++' : 'enum ID' does not define this operator or a conversion to a type acceptable to the predefined operator

How can I get this to behave the same in 6.0?

+2  A: 

Please try to convert to int, add one (+1) and convert back to the enum.

Pavel Radzivilovsky
@Pavel: any good methods to convert? Is this suggested? http://stackoverflow.com/questions/367819/enum-int-casting-operator-or-function/368050#368050
Tommy
enumTypeVariable = (enumtype)(1+(int)enumTypeVariable)
Pavel Radzivilovsky
Adding 1 to an enum represented as an int doesn't guarantee that you will have another valid enum value.
Greg Domjan
@Greg: Can the enum be converted to some other C++ data structure with similar behavior?
Tommy
@Greg: Valid enum values, i.,e. values that can be "forced" into a enum object include all integral values in the enum range rounded to the nearest greater power of 2. In other words, just because a value you obtained doesn't have a name in the enum definition, does not necessarily mean that the value is invalid. As long as you stay within the range, all values are valid.
AndreyT
+4  A: 

an enum may be intergral but it doesn't mean it covers a continuous range.

This

enum {
  A, 
  B,
  C,
}

May Will default to

enum {
  A = 0, 
  B = A + 1,
  C = B + 1,
}

and so you could get away with

int a = A;
a++;

However if you have

enum {
  A = 2, 
  B = 4,
  C = 8,
}

now +1 ain't gonna work.

Now, if you also had things like

enum {
  FIRST,
  A = FIRST, 
  B,
  C,
  LAST = C
}

then when iterating the enum would you do A and C twice?

What is the purpose of iterating the enum? do you wish to do 'for all' or for some subset, is there actually an order to the enum?

I'd throw them all in a container and iterate that instead

  • unordered - use a set
  • ordered - a vector or list
Greg Domjan
Thanks. It is keeping track of some IDs as I read a file in. The enum is ordered 0-95 with a valid var for each value. I will check out a vector list next.
Tommy
Just one quibble. It's not that `enum { A, B, C},` **may** default to `enum { A=0, B=A+1, C=B+1,}` but that it **will** default to to those values (if it doesn't, it would not be conforming).
R Samuel Klatchko
+5  A: 

I see nothing wrong with defining operator++ on a well understood enum. Isn't that the purpose of operator overloading? If the context made no sense (e.g. an enum with holes in it), then of course it doesn't make sense. Defining operator* for a class called Complex that implement complex numbers is not just valid but a great application of mathematical operator overloading in C++!

If the developer defines an enum where operator++ makes obvious and intuitive sense to the clients of that enum, then that's a good application of that operator overload.

enum DayOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
inline DayOfWeek operator++(DayOfWeek eDOW, int)
{
   const int i = static_cast<int>(eDOW);
   return static_cast<DayOfWeek>((i + 1) % 7);
}
franji1
@franji1: I was just going to ask this question (but searched first). I'd like to use some enums to distinguish integers used for various things, to prevent accidental cross assignments (e.g. between physical and virtual block numbers) but still allow 'for' loops. Defining the operators that otherwise don't work with enum is a perfect solution.
supercat