C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 10 Jul 2023 10:59:33 +0200
pon., 10 lip 2023 o 10:03 Jonathan Wakely via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
>
>
> 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.
>
> So you need to give some actual rationale for your proposal.
>

Sometimes you can use enum as specific flags and consider undeclared values UB.
Like:
```
enum ResourceTypes : signed char
{
   None = -1,
   HP = 0,
   Mana = 1,
   Stamina = 2,
};

std::array<int, std::enum_max<ResourceTypes >> resources;
```
Value 3 is useless for me in that case. I can cast it to enum but it
will break something.

Probably better contr argument is empty enums:
```
enum class X {};
```
it does not have min or max.


Beside this proposal is pointless because at some point reflection
could allow achieving this
by enumerating all declared values in enumeration.
Not mention multiple other proposal that try add similar functionality
that do not rely on reflection
and allow some basic introspection into enums.

>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-07-10 08:59:43