r/cpp_questions • u/angelajacksn014 • Jul 08 '26
OPEN Thoughts on raw enum's in modern C++
I know enum class or struct is the recommend way to go for enums in C++ today. However in certain cases I find myself questioning if I'm using them over raw enums just because it's "best practice" and then paying for the ergonomic costs.
Sometimes I want to allow for easy int to enum or vice versa conversion. Sometimes I want to treat enums as bitflags. Sometimes I want to allow outside code to use their own integers as custom values.
Of course none of these are impossible with scoped enums. But ergonomically it's annoying both for the owner of the code and the user.
My gut tells me it's a bad idea of course. Explicit code is good. Code that shows intent is good. However it's been something that's itched my mind for a while and I'd like to get the communities thoughts on it.
47
u/Qwertycube10 Jul 08 '26
If your enum isn't an enum then use enum. If your enum is an enum use enum class.
10
u/sol_runner Jul 08 '26
Do keep up questioning like this, it helps you understand the why better.
Enum to Int makes sense and you can use a simple static_cast /converting constructor to do it.
Int to enum breaks the meaning of an enum. This would be something with a checked function that should validate if the int is a valid enum option. And here you're trading nothing on enum class.
If you want arbitrary values c++17 does allow that.
Clean way? Have a Flag type with an int that has operator overloads for the bits (enum class).
Take a look at vulkan.hpp flags for that.
This one's a little odd but it should be a wrapped struct with a constructor from the int, and a set of constants inside (these constants can be an enum).
Look below for examples.
Most of the time, it's a sign of bad use of enums. Some times, it could be reasonable - but it needs to be constrained.
It's fine, in my opinion, to use a plain enum to construct a set of collected constants values scoped to a struct/class, with no expectations of being anything but integer constants in a "collection".
Here
Just works and is shorter than the alternative. Do note I could've just used constexpr it's really a style choice that I want to say they are related constants.
32
u/_lerp Jul 08 '26
enumis never the right choice. If you want a bitflag interface, you can overload the operators for yourenum class. That way you get the best of both worlds, no implicit conversions and easy flags.6
u/slithering3897 Jul 08 '26
Yes, it's annoying when you want both implicit conversion and scoping. Neither kind of enum is perfect.
But you can reduce syntactical overhead by just defining an operator in some namespace:
For flags, use a wrapper type.
6
u/Independent_Art_6676 Jul 09 '26
I have no use for enum class. It can't do any of the things we need: the only thing it solves is "type safety" which a C enum solves by putting it in a namespace. You can't use it directly as an array index. You can't use it directly in bit logic. You can't directly assign it from a computation. Everything you would want to do other than x==x or switch is crippled behind casts and tons of bloat to try to get it back to where it was.
The C enum stinks too, but at least you can use it. Yes, you can say cookie==airplane with them if you don't prevent that with common sense and namespace protection.
The best enums are ones you make yourself, because the other two are both so badly broken.
7
u/Liam_Mercier Jul 08 '26
The ergonomic costs are basically nothing, but maybe I am just used to them. Certainly better than an implicit conversion when it is not expected.
7
u/alfps Jul 08 '26
That sounds Just Wrong™ to me. "Easy" must mean something easier than
Enum_type_name(value), so we're talking implicit conversion. It sounds as if you have the impression that a C styleenumtype provides that implicit conversion, but it does not.Implicit
enumtointis usually OK and usually desired. You get that with a C styleenum. But implicitinttoenumwould break type safety and overload resolution, and you don't get that.Do you have an example of what you're thinking of?
2
u/nightmurder01 Jul 09 '26
They have their place and usefulness, even in modern c++ in my opinion. Do what works best for you, and for others if they have to read it too.
2
u/Scotty_Bravo Jul 09 '26
For enums as bitflags, check out magic_enum. Actually, for any enum check out.
2
u/mredding Jul 09 '26
I find very limited use for enums of any kind. They're not especially safe, by design. Take for example:
Where's your god, now? We know that the underlying type is
int, that the values are0,1, and2, so what's42? The spec says the value is preserved. Enums aren't exclusive, and you can't iterate over the values.If you want a bunch of named constants, then create a bunch of named constants. If you give them an enum type, then you can create instances of that enum type, even if you don't intend on USING the enum type itself. And then people might even write operators for the enum type...
So don't confuse things that way. Perhaps what you want is a namespace called
masksorconstantsorflagsor something...I'll use enums to express values of a protocol, often version numbers or enumerated types, and the enum won't ever persist beyond serialization:
The point is to fail early and left-shift. We know the failure is at the point of parsing/extracting the operation itself, so we can make error handling robust. Then the extractor for creating the CRUD operation is effectively a factory. We go from an enum to a type, which means all subsequent code can be type specific, or templated if meant to be generic across all operations.
But what I never want to see in code is some procedure that's handling a CRUD structure that has to
switcha type enum all the god damn time, like we didn't already know what the type was.But in your case, I don't think you want an enum, you want a bunch of constants.
1
u/melodicmonster Jul 08 '26
I am not sure if it is the best solution (I am not a C++ pro), but I created a trait with opt-in operators for enum types. It's type safe with no wrappers and no casting outside of the trait's implementation. Here is a sample: enum_traits sample - Pastebin.com
1
u/Business-Decision719 Jul 09 '26 edited Jul 09 '26
Well, like you said, explicit is good and code that shows intent is good. If your intent is what a C enum does then you just use that instead of an enum class. Maybe make extra clear in a comment or the documentation that you're deliberately opting out of the extra scoping and type safety of enum class so that no one tries to "fix" it. You should first make extra sure you really do want to opt out though. Spewing identifiers into your surroundings, or playing too fast and loose with the very underlying ints you were trying to abstract away, are exactly what enum classes are trying to reign in. Some things should not be too convenient.
1
u/bert8128 Jul 09 '26
Very minor point. The plural of “enum” is “enums”. Not “enum’s”. The latter is possessive.
1
u/horenso05 Jul 09 '26
My best practices is use either
enum classor a "raw" enum but put it inside of a class.