Discussion:
C23 enums
(too old to reply)
Thiago Adams
2024-06-15 09:04:58 UTC
Permalink
I realized we can have "private" enums in C23, because they are
completed types. Values of E does not need to be at header file.

enum E : int;

struct X{
enum E e;
};
Lawrence D'Oliveiro
2024-06-18 07:00:27 UTC
Permalink
Post by Thiago Adams
I realized we can have "private" enums in C23, because they are
completed types. Values of E does not need to be at header file.
enum E : int;
struct X{
enum E e;
};
What would be the point of this?
Thiago Adams
2024-06-18 11:09:13 UTC
Permalink
Post by Lawrence D'Oliveiro
Post by Thiago Adams
I realized we can have "private" enums in C23, because they are
completed types. Values of E does not need to be at header file.
enum E : int;
struct X{
enum E e;
};
What would be the point of this?
This creates a kind of encapsulation. Including the header file will not
include the values and consequently the usage of e is discouraged.
Lawrence D'Oliveiro
2024-06-19 07:24:09 UTC
Permalink
Post by Thiago Adams
This creates a kind of encapsulation.
Why not just use “int” or “unsigned int”? Why does the caller even need to
know it’s an enum?
Thiago Adams
2024-06-19 12:38:24 UTC
Permalink
Post by Lawrence D'Oliveiro
Post by Thiago Adams
This creates a kind of encapsulation.
Why not just use “int” or “unsigned int”? Why does the caller even need to
know it’s an enum?
The advantage will be the type check at the implementation file (some
compiler have extra type check for enuns) and clarity about the possible
values accepted.
Lawrence D'Oliveiro
2024-06-20 00:50:09 UTC
Permalink
Post by Thiago Adams
Post by Lawrence D'Oliveiro
Post by Thiago Adams
This creates a kind of encapsulation.
Why not just use “int” or “unsigned int”? Why does the caller even need to
know it’s an enum?
The advantage will be the type check at the implementation file (some
compiler have extra type check for enuns) ...
Obviously this is not a language requirement, so I’m not sure of the
point. E.g. GCC compiles this rather flagrant example without complaint:

enum colours {red, green, blue};

enum colours colour = 9;

I even tried “-Wall -Wpedantic”, to no effect.
Post by Thiago Adams
... and clarity about the possible values accepted.
As a documentation aid? Again, that’s implementation-specific, nothing to
do with the caller-visible interface.

Loading...