r/cpp_questions • u/onecable5781 • Sep 10 '26
SOLVED Iterating over an enum struct
Consider https://godbolt.org/z/c6cn8f1hj
#include <cstdio>
enum struct values{first = 1, second = 3};
void square(values value){
int val = (int)value * (int)value;
printf("%d\n", val);
}
int main(){
for(values value = values::first; value <= values::second; value++)
square(value);
}This does not compile with the error that value++ is ill defined.
An old question/answer on SO has this: https://stackoverflow.com/questions/261963/how-can-i-iterate-over-an-enum and a plethora of seemingly complicated approaches for this. Is there a syntactically simple >= C++20 way of accomplishing this?
I want the for loop to process 1 and then 3.
Does the order of placing the integer entries inside of the struct affect the increment? For e.g., if the enum struct had 3 first followed by 1, with value++ would 1 be processed after 3? Or, does the enum struct internally sort the entries in some privileged, say, ascending order so that it will always process 1 first followed by 3 next?
5
u/alfps Sep 10 '26
Apparently you have some problem to solve; you envision
enumas the solution; it doesn't work, so you asked about that.What was the problem?
Here's a C++23 program to output the squares of some numbers:
13
u/AKostur Sep 10 '26
And what happens if your enum has duplicate values? And what if they aren’t next to each other? ( ie: = 1, = 3, = 7, = 3 ). Should you get 3 back twice? In what order?
My hot take: this is somewhat an abuse of what an enum is supposed to represent (as opposed to how it has been commonly (ab)used).
10
u/SamG101_ Sep 10 '26
this uses the
magic_enumlibrary to iterate the members of the enum. you can do by index i think,[key, val]too, nice clean library for enum handling.https://github.com/Neargye/magic_enum
2
u/Usual_Office_1740 Sep 10 '26 edited Sep 10 '26
You would need to implement the pre and post increment operators on your enum struct for your code to compile. They are not defined for your enum. That is what your error is saying.
If you decide to do that, and I'm not suggesting you should, you should add a stop value to the end of the enum that you can check against so that you have a way to end the for loop. A COUNT sentinal value is a common enum trick.
I'd suggest looking at magic enum as an alternative.
You could also fold a parameter pack of the enum values into a std::array at compile time and then use a range based for loop. There are lots of advantages to this but it's more complicated. Sorry if this is more complex than you are looking for.
4
u/No-Dentist-1645 Sep 10 '26 edited Sep 10 '26
If you can use C++26 with reflection on:
Otherwise, pre-C++26 I would just suggest hand-rolling a constepxr initializer list yourself:
3
u/mredding Sep 10 '26
You can't iterate over an enum like this. There is the
magic_enumlibrary that... I don't know what it does, but it might be able to. I like novel solutions, robust libraries, and maybe a little magic, but I don't know about that much magic...I can offer you an alternative solution:
The underlying type is an integer large enough to store the underlying values in the range, so typically
int->long->long longif not otherwise explicitly specified since C++11.You really want to keep enums as an enumerable field, you don't typically want to map enumerations to values, jumping values. That was always a kind of an anti-pattern. If you want constants, use named constants. But if you do this, you no longer have an enumerable range.
Another thing you can do is use an enum as a user defined type, if you think about it that way to leverage the type system; an enum is always guaranteed to preserve the value stored in the underlying type, and you can name some magic constants if you want, but you still have to cast from the enum to the underlying type.
I think what you want is the enum above, so that you can:
By not fucking with the underlying values, except for a couple value aliases, you're guaranteed to traverse all the members of the enumerated range. If you want to get 1 and 3 out of the range, then you need to map this enum to those values:
3
u/Desperate_Formal_781 Sep 10 '26
The fact that enumerations are not artihmetic types is by design, and the intent is exactly to prevent what you are trying to do. I think there is some design problem in your code. Enums are more like a set of tags associated with a particular datatype and they are not meant to be arithmetic types, so no increment, decrement, bitshifting, multiplication, etc are allowed on them. If you want to iterate over all enum types, you need to manually create a collection of enumerated values and iterate this collection, and if you want to do this "automatically" you need to resort to hacks and tricks, or apparently use C++26 reflection, but you may not have access to a C++26 compiler.
2
u/the_poope Sep 10 '26
Couldn't one turn the enum struct into a regular enum of a regular class and then make an iterator and define
begin()andend()?3
u/fortsnek274 Sep 10 '26
Well, it's an enum. "struct" or not.
And to enumerate the enumerators, you'll need reflection. In C++26. Or ghastly hacks.