... Does C++ not allow me to increment an enum using ++? :psyduck:

And I can't do ((int)the_enum)++, either

That's stupid. C never gave me these kinds of problems. Enums are fancy numbers. Let me use them as numbers.

Everything inside the computer is a fancy number

And it doesn't accept the_enum = 1 + (int)the_enum, either? :psyduck:

Are you TRYING to make me write this code in an uglier way where the_enum is an integer instead of an enum?!

If any C++ knowers know a workaround for this, please let me know

Well, I passed the compiler -fpermissive and now it works (but still prints a warning), but that's not great, especially since ++ still doesn't work

How do I make enums behave exactly like in C?!

Hello @wallhackio the C++ knower, I need C++ help. Is there any way to make enums behave exactly like in C, or at least not complain about increment? Here's a minimum example of what's NOT working:

int main() {
enum gorp { FOO, BAR, BAZ } gorp = FOO;
gorp++;
}

The compiler rejects it:

$ LC_ALL=C g++ -fpermissive test.cpp
test.cpp: In function 'int main()':
test.cpp:3:7: warning: no 'operator++(int)' declared for postfix '++', trying prefix operator instead [-fpermissive]
3 | gorp++;
| ~~~~^~
test.cpp:3:7: error: no match for 'operator++' (operand type is 'main()::gorp')

I can replace gorp++; with gorp=(int)gorp+1;, and then it'll compile iff I set the -fpermissive option, but I don't like that because it's uglier and less readable than gorp++

@aescling @wallhackio "Implicit casts" meaning that when I do e.g. int *ptr = malloc(whatever) and malloc returns a void*, then it gets automatically casted to int*? And that an enum gets casted to an integer because it is an integer? Yes, I do like that

@vaporeon_ @wallhackio imo “enums are exactly the same as ints” is exactly the opposite of the point of having an enumerated type

@vaporeon_ @wallhackio i’m curious now, when you were learning haskell, did type definitions like

data Whatever = Thing1 | Thing2

ever bother you in the way that the value of Thing1 is kind of abstract? i remember (the equivalent to )this taking a while fur me to get used to when i was furst learning OCaml

@aescling @wallhackio But now that you mention it, I do want to know... How do they represent it internally?

Follow

@vaporeon_ @wallhackio in OCaml, variants without parameters are unboxed ints. variants with parameters are a little bit more complex

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.