1

Referencing this question, I ask, what is the best way (as in general, least error prone approach) to iterate over all enum values in C++98?

Limitations of C++98:

  • no enum classes
  • no foreach loop

What should be avoided in the solution (I am pretty sure, not everything can be avoided):

  • A) Macros masking standard language features (e.g. #define CREATE_ENUM(...))
  • B) including other headers
  • C) repetition of code
  • D) adding enum elements only for iteration
  • E) assumption on enum layout (e.g. always increments by 1, last element is always XXX)
kuga
  • 1,483
  • 1
  • 17
  • 38
  • 1
    This question (and the linked question as well) seems to be based on the wrong assumption that all possible enum elements are those listed between `{ }` when enum is defined. – user7860670 Jan 05 '21 at 09:20
  • @user7860670 Interesting! Can you provide an example of how to specify an enum element later? – kuga Jan 05 '21 at 09:23
  • For example if you have `enum t_Values { a /* 0 */, b /* 1 */, c /* 2 */ };` then 3 will be a valid enum value as well even though it is not listed. – user7860670 Jan 05 '21 at 09:26
  • Well, if we accept everything that fits into enum we could just loop from INT_MIN to INT_MAX. My intention is indeed, *iterate over all elements stated in de enum definition*. – kuga Jan 05 '21 at 10:00
  • *"if we accept everything that fits into enum we could just loop from INT_MIN to INT_MAX"* - not really. Iterating over named values listed in enum definition is no different from iterating over any other group of known values of any type. – user7860670 Jan 05 '21 at 10:17
  • @kuga: There's no reason to assume that the lower bound is `INT_MIN`; it could be larger or smaller. Equally, the upper bound could be smaller or larger than `INT_MAX`. You need the underlying type to know the bounds, and that's not always `int`. – MSalters Jan 05 '21 at 14:32

2 Answers2

1

There is no way to achieve all of your A..E. You can achieve:

  • BCDE by using macroes (e.g. #define CREATE_ENUM(...)).
  • ABDE by storing all elements in an array
  • ABC by only using consequtive starting from 0 and adding extra element to represent the count (as shown here) or more generally, values that can be represented as a mathematical sequence.

Note that neither enum classes nor range-for loops bring anything that would widen your choices as far as these points are concerned in the current C++ standard.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Usual trick of that time was to do this:

enum Foo {
  One,
  Two,
  Three,
  Last,
  MAX_FOO_ENUM
};

for( int it = One; it < MAX_FOO_ENUM; ++it )
...

Of course it works correctly correct only if there is no skips in Foo. There is no way you can do that correctly otherwise unless you define some kind of container with elements in it equal to each value of Foo, but it's true for later standards too.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Not criticizing (this is a valid solution), but just to point it out: It fails points D and E. – kuga Jan 05 '21 at 09:25
  • @kuga C, D were solved in C++11. in C++98 they are unsolvable, E essentially unsolvable unless you would use some kind of template to iterate through values. It's like trying to solve lack of variadic templates. Microsoft did that to `std::tuple`. By macroses and limiting tuple to 16 elements. :P – Swift - Friday Pie Jan 05 '21 at 09:46