iterate through non-consecutive enum elements in c++

c++, enums

Solution

The short answer to this is "no".

You could make a table `animals`, and then use a range loop on `animals`.

Here's a complete "demo":

#include <iostream>

using namespace std;

enum Animal {Cat = 0, Dog = 5, Dolphin = 8};

int main()
{
    Animal animals[] = { Cat, Dog, Dolphin };

    for(Animal a : animals) cout << a << endl;
}

The output will be:

0
5
8

Problem

There are many ways to iterate through consecutive enums like ``` enum Animal {Cat, Dog, Dolphin} ``` But is there a convenient and easy way to iterate through non-consecutive enum elements like ``` enum Animal {Cat = 0, Dog = 5, Dolphin = 8} ```

Original source

Related problems