Date: Mon, 10 Jul 2023 13:25:10 +0200
On 10/07/2023 10:53, Alejandro Colomar via Std-Proposals wrote:
> Hi Jonathan!
>
> On Mon, Jul 10, 2023, 10:03 Jonathan Wakely via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
>
>
> On Mon, 10 Jul 2023, 08:15 Frederick Virchanza Gotham via
> Std-Proposals, <std-proposals_at_[hidden]
> <mailto: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.
>
Just to be clear - "auto e = (MyEnum) INT_MAX;" is legal C++, but it
does not do what the OP wanted. It gives you a variable of type
"MyEnum" with value INT_MAX, because an "enum" type in C++ is basically
just a different name for an "int" (or other underlying integer type).
The language does not restrict the values that can be stored in an
enumerated type, beyond the limits of the underlying integer type.
And since it is perfectly legal to have a "MyEnum" variable with a value
of INT_MAX, the "enum_max" of an enumerated type is simply the maximum
value of the underlying type.
I am sure a lot of people (including me) would like strong enumeration
types that may only (by defined behaviour) hold the values given in the
enumeration, and then it would sometimes be very useful to have a
minimum and maximum value. But we don't have that at the moment - it
may have to wait for reflection and/or metaclasses. Until then, I don't
think a standardised enum_min and enum_max function could make sense.
(It might be feasible to have these as compiler-specific extensions.
After all, many C++ compilers can give helpful warnings about assigning
arbitrary values to enumerated types, so they have the required metadata
on hand. But I can't see how that would be standardised, and such
extensions would be outside of the scope of this list.)
> 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.
> Hi Jonathan!
>
> On Mon, Jul 10, 2023, 10:03 Jonathan Wakely via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
>
>
> On Mon, 10 Jul 2023, 08:15 Frederick Virchanza Gotham via
> Std-Proposals, <std-proposals_at_[hidden]
> <mailto: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.
>
Just to be clear - "auto e = (MyEnum) INT_MAX;" is legal C++, but it
does not do what the OP wanted. It gives you a variable of type
"MyEnum" with value INT_MAX, because an "enum" type in C++ is basically
just a different name for an "int" (or other underlying integer type).
The language does not restrict the values that can be stored in an
enumerated type, beyond the limits of the underlying integer type.
And since it is perfectly legal to have a "MyEnum" variable with a value
of INT_MAX, the "enum_max" of an enumerated type is simply the maximum
value of the underlying type.
I am sure a lot of people (including me) would like strong enumeration
types that may only (by defined behaviour) hold the values given in the
enumeration, and then it would sometimes be very useful to have a
minimum and maximum value. But we don't have that at the moment - it
may have to wait for reflection and/or metaclasses. Until then, I don't
think a standardised enum_min and enum_max function could make sense.
(It might be feasible to have these as compiler-specific extensions.
After all, many C++ compilers can give helpful warnings about assigning
arbitrary values to enumerated types, so they have the required metadata
on hand. But I can't see how that would be standardised, and such
extensions would be outside of the scope of this list.)
> 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.
Received on 2023-07-10 11:25:20