How awful is it – or is it perfectly acceptable – to index a loop on an enumeration?
I have an enumeration defined. The values of the literals are default values. The assigned values do not have any significance, will not have any significance, and the values of any literals added in the future will also not have any significance. It’s just defined to limit the allowed values and to make things easier to follow. Therefore the values will always start at 0 and increase by 1.
Can I set up a loop like so:
enum MyEnum
{
value1,
value2,
value3,
maxValue
}
for(MyEnum i = value1; i < maxValue; i = static_cast<MyEnum>(i+1)){}
As far as I’m concerned, that’s just fine. I’m sure some purist out there somewhere will freak out, but as far as the language spec is concerned, that code will work correctly, so you should feel free to go for it if it makes your life easier.