C++ Logo

std-proposals

Advanced search

[std-proposals] Fwd: std::enum_max and std::enum_min

From: Alejandro Colomar <alx.mailinglists_at_[hidden]>
Date: Mon, 10 Jul 2023 11:25:35 +0200
Sent from wrong email address to the list. Oops.

---------- Forwarded message ---------
From: Alejandro Colomar (man-pages) <alx.manpages_at_[hidden]>
Date: Mon, Jul 10, 2023, 11:24
Subject: Re: [std-proposals] std::enum_max and std::enum_min
To: <std-proposals_at_[hidden]>
Cc: Sebastian Wittmeier <wittmeier_at_[hidden]>


Hi Sebastian!

On Mon, Jul 10, 2023, 11:08 Sebastian Wittmeier via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hi Alex,
>
> I think you misunderstood.
>
> Jonathan's INT_MIN / INT_MAX do not return 1 or 16, respectively, for
> enums.
>

Yep, I misunderstood. :)
I was very surprised that there was some magic that converted INT_MAX into
16. Of course there isn't.

Quite the opposite, Jonathan pointed out that the existing values INT_MIN /
> INT_MAX can be stored in MyEnum, so 1 or 16 are _not_ the minimum or
> maximum possible value for the example.
>

>
> This has often used practical applications:
>
> MyEnum wolpertinger = eMonkey | eFish | eGoat | eFrog; // 30 > 16
>
>
>
> e.g. if like in the example the enum values are given as power of 2 flags,
> which can be added/ORed.
>

Yeah, that's true. Although in C I never liked using enums for flags, due
to not being able to use unsigned types, in C++ (and C23) it makes sense to
group them with an enum.

Thanks!
Alex


>
> Best,
>
> Sebastian
>
>
>
>
> -----Ursprüngliche Nachricht-----
> *Von:* Alejandro Colomar via Std-Proposals <std-proposals_at_[hidden]
> >
> *Gesendet:* Mo 10.07.2023 10:53
> *Betreff:* Re: [std-proposals] std::enum_max and std::enum_min
> *An:* std-proposals_at_[hidden];
> *CC:* Alejandro Colomar <alx.mailinglists_at_[hidden]>;
> Hi Jonathan!
>
> On Mon, Jul 10, 2023, 10:03 Jonathan Wakely via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>
>
> On Mon, 10 Jul 2023, 08:15 Frederick Virchanza Gotham via Std-Proposals, <
> std-proposals_at_[hidden]> wrote:
>
> Given the following enum:
>
> enum MyEnum : int {
> eInvalid = 1,
> eFrog = 2,
> eGoat = 4,
> eFish = 8,
> eMonkey = 16,
> };
>
> I propose that:
>
> std::enum_max<MyEnum>
>
> would evaluate to 16, and that:
>
> std::enum_min<MyEnum>
>
> would evaluate to 1.
>
>
> Why?
>
> This is perfectly valid:
> auto e = (MyEnum)INT_MAX;
> And similarly for INT_MIN and 0 etc.
>
>
> Heh, I learnt something new today. I didn't know about this. Would you
> mind pointing to where this feature is described in the standard? I'm
> curious about it.
>
> Regarding the proposal, I find it more readable and portable, since
> casting to an enum type in C doesn't do that magic. But since the feature
> already exists, I wouldn't support yet another way of doing the same thing.
>
> At the very least, I'd use this cast always within a template or macro
> called to_enum<type>(value) or to_enum(type, value). But probably in a
> project-defined one, and not a standard one.
>
> Cheers,
> Alex
>
>
>
> So you need to give some actual rationale for your proposal.
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-07-10 09:25:47