tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: Do all -current compilers support C23 typed enum syntax?
On Dec 3, 2026, at 3:57 PM, Greg Troxel <gdt%lexort.com@localhost> wrote:
> All in all, this feels like too much complexity imposed on everyone to
> solve a perhaps-theoretical safety problem.
It’s not a theoretical problem, it’s just one that you may not have run into.
We needed this at Apple because we used enum extensively in APIs, we required ABI stability even in the face of developers needing to pass options like -fshort-enums (to port code from other platforms without extensive and invasive modification), and we got lots of negative feedback from developers on both the compiler’s lack of checks for assigning incorrect enum values and on the debugging experience with enum values.
Explicitly-typed enums are strictly an improvement, and at some point it will be safe to use them in APIs; I was really just wondering whether now was that point.
I can always just use a macro like this to handle both situations:
#if /* the compiler supports explicitly-typed enums */
#define MY_ENUM(t,n) enum n : t; enum n : t
#else
#define MY_ENUM(t,n) t n; enum
#endif
This was our solution at Apple; if the compiler supports the feature use it, if the compiler doesn’t support it just do things the old-fashioned way with an int and anonymous enum. The declaration sites then look like this:
typedef MY_ENUM(int,my_option_t) {
some_option_a = 1,
some_option_b = 2,
};
which isn’t all that messy and reads more like a usual typedef.
It sounds like this is the approach I should take in what I’m hacking on in order to enable compilation in older language modes.
-- Chris
Home |
Main Index |
Thread Index |
Old Index