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 4:58 AM, Anders Magnusson <ragge%tethuvudet.se@localhost> wrote:
> can you explain in which situation you need to modify the type of an enum?  Since it is supposed to be (representable as) an int?

I explained briefly in another response, but without an explicit type, the type used to represent an enum is up to the compiler. It’s int by default, but doesn’t have to be, and can be changed by things like compiler arguments (to e.g. prefer the narrowest integer type, or the widest).

And if you want to actually get compiler enforcement for invalid assignments and a good debugging experience, you actually need to use the enum name (or a typedef) in structure members, variables, and arguments.

These combine to mean that

    enum call_options { a = 1, b = 2, };
    int some_call(enum call_options options);

does not get a stable ABI, but one that can change based on compiler arguments. Using “enum call_options : int” tells the compiler how to represent the type in a way that cannot be overridden. And the above is strictly superior to

    enum { a = 1, b = 2, };
    int some_call(int /* see above */ options);

because just using int and a comment doesn’t get any compiler enforcement or debugging affordances.

  — Chris



Home | Main Index | Thread Index | Old Index