r/cpp • u/ralseieco • Jun 25 '26
Am I the only one who thinks this? (about enum)
The main differences between an enum and enum class|struct is that the latter has its own scope, but you lose the implicit operators from enum, and the closest to it is as presented. I wish that enum struct maintained the operators, acting like the C enum, but with it's own scope.
// Dummy wrapper struct
struct EnumName
{
enum Value
{
ZERO
};
// the scope is located in "EnumName"
// so you access "EnumName::ZERO"
};
41
Upvotes
111
u/Due_Battle_9890 Jun 25 '26
I use enum classes for type safety tbh
73
u/No-Dentist-1645 Jun 25 '26
Implicit operators are the very reason why enum class was designed like it is, to avoid them. Modern programming practices heavily recommend against implicit conversions.
If you do want implicit conversions, there's nothing wrong with using C enums inside a dedicated namespace
29
u/LordofNarwhals Jun 25 '26
If all you want are the scope semantics, can't you just wrap an anonymous enum I'm a namespace?
9
u/fdwr fdwr@github 🔍 Jun 25 '26
There are so many useful integral cases for enumerations, such as bitflags, named indices for a list of named array indices (so instead of saying
input[1]andinput[2], you could more enlighteningly sayinput[TensorIndex::Filter]andinput[TensorIndex::Bias]), and other cases, all of whichenum classinhibits. So on one hand, you have classicenumwhich conveniently works with bitwise operators and as array indices, but spills its enumerant guts into the outer scope 🥲, and on the other you haveenum classwhich wonderfully keeps its guts nicely scoped but then inhibits use cases of enum -> base class 🥲. Note, both forms of enumeration wisely warn (in most compilers these days) about the opposite direction, implicit integer to enum conversions. Surprisingly though,enum class MyEnum : uint64_tdoes not behave like normal theclasssuch asclass Dog : Animalwhere you can safely pass aDog&to a function expecting anAnimal¶meter, as passingMyEnumto a function expectinguint64_twill fail without explicit cast. I just wish there was a concise best-of-both-worlds likenamespace enum Foothat permitted enum -> integer base type casts while also keeping enumerants scoped. So imagine condensing this:To:
6
u/KindCppCoach Jun 26 '26
If you want that, put a regular enum inside a struct or namespace. However, having no implicit conversions has caught lost of bugs to disappear, and you can selectively make your own conversations, so I don't really see any drawnbacks.
2
u/Onlynagesha Jun 26 '26
I'd prefer the
enum_class+using enumpattern for both type safety and convenience.This is also the case of
std::meta::operatorsin C++ standard library:3
u/Initial-Jaguar6230 Jun 25 '26
Doesn't namespace is what you search for?
3
u/celestabesta Jun 26 '26
I want the opposite. I want an enum with strong typing but loose scoping, such that I can have an enum within a struct and refer to the enum's members using the struct's scope. I do this sort of thing often for structs that store a 'type' field.
2
u/torrent7 Jun 25 '26
You can do this trivially with various libraries
2
u/JVApen Clever is an insult, not a compliment. - T. Winters Jun 25 '26
I honestly don't get why you would like to have the operators defined on the enumerations. The only use-case I see are flag enum, which you really should not write. Use a std::bitset with the enum as index. Write a small wrapper to cast.
1
u/_yrlf Jul 04 '26 edited Jul 04 '26
The general mechanics around scoped vs unscoped enums are really weird. They change a lot of things at the same time in a way that makes the two feel like VERY distinct features:
Changes between
enumandenum struct/class, AFAIK:1
u/archialone Jul 21 '26
You are not the only one.
You can add bitmask operators by following this post https://andreasfertig.com/blog/2024/01/cpp20-concepts-applied/
And i created a proposal exactly for that https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p4313r0.html
0
u/[deleted] Jun 25 '26
[deleted] — view deleted comment