Loop on enumeration values

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-03-19T18:38:38Z Indexed on 2010/03/19 18:41 UTC
Read the original article Hit count: 164

Filed under:
|
|

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)){}

© Stack Overflow or respective owner

Related posts about c++

Related posts about enums