Sujet : Re: enum sets
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 29. Aug 2024, 12:23:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vaplni$3ullf$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 28/08/2024 21:30, Keith Thompson wrote:
Thiago Adams <thiago.adams@gmail.com> writes:
I am wondering how useful would be to have enum sets.
>
Let´s say you have a function that accepts only monospaced fonts.Then
you can use enum monospaced_font_type. Or a switch case where you need
to check all and only monospaced_font_type.
>
But at same type you can store at same object monospaced_font_type or
font_type.
>
enum font_type
{
enum monospaced_font_type
{
CASCADIA_FONT,
},
ARIAL_FONT
};
>
This could be arranged in any way.
If I understand you correctly, enum monospaced_font_type is a *subtype*
of enum font_type.
Ada has something very similar to this:
type Font_Type is (CASCADIA_FONT, ARIAL_FONT);
subtype monospaced_font_type is Font_type range CASCADIA_FONT .. CASCADIA_FONT;
An Ada subtype is a type with an optionally added *constraint*,
which can be checked at runtime. But it's hard to see how you'd
add this to C.
Given your type definition, how would this behave?
void func(enum monospaced_font_type);
enum font_type font = ARIAL_FONT;
func(font);
(The Ada equivalent would raise a run-time exception.)
Warning because not all font_type are monospaced_font_type.
void func(enum monospaced_font_type);
enum font_type font = ARIAL_FONT;
func(font);
(But all monospaced_font_type are also font_type.)