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 Thu, 3 Dec 2026 at 08:12, Mouse <mouse%rodents-montreal.org@localhost> wrote:
>
> > For example:
>
> >     typedef enum : int {
> >             subsys_thing_a = 0,
> >             subways_thing_b,
> >     } subsys_thing_t;
>
> >     int subsys_call(subsys_thing_t thing);
>
> What benefit does this bring?  enum constants have historically been
> ints (I don't _like_ this - it feels to me like conceptual type
> confusion - but it's how they've worked), so, unless you anticipate
> wanting an argument that is smaller or larger than int, I see no value
> in attaching a specific type to it.  Even if you do, all you need is to
> document the argument as having its actual type.  The only real
> difference I see between
>
>         enum {
enum myenum {
>                 FOO = 1,
>                 BAR,
>                 BAZ,
>         };
>
> and
>
>         #define FOO 1
>         #define BAR 2
>         #define BAZ 3
>
> is that they obey different scope rules.  You can pass either one to an
> argument of any integral type just fine (well, possibly except _Bool).
>
> What am I missing?

(somewhat off topic; I thought GCC enums were unsigned by default)

Given:

enum myenum { MA, MB, MC, };
enum notmyenum {NB, NA, NZ, };

then given:
   myfunc(enum myenum e);
compilers can warn about the mismatch:
   myfunc(NB);
Because of the really an int/unsigned rule, it's all a bit loosy
goosy, but better than nothing.


Home | Main Index | Thread Index | Old Index