Date: Wed, 10 Aug 2022 15:35:31 +0300
>
> Scoped enumerations require a `static_cast`to cast between
> enumerators("enumeration values") and the value types. However, they
> provide little safety checking, and can return undefined values in case of
> failure.
> I propose a new cast `enumeration_cast` that throws an exception if the
> cast fails(no matching enumerators) instead of returning undefined values.
> Moreover, if the enumerator types do not match or the cast parameter is
> `constexpr`(known at compile time) and the cast cannot succeed, the program
> shall be ill-formed.
>
There is already, in C++23, std::to_underlying
<https://en.cppreference.com/w/cpp/utility/to_underlying> which casts the
enum to the correct underlying type without the need for the user to
specify it explicitly.
About the exception throwing in case of invalid value, I think it will need
some kind of reflection in order to get the possible enumeration values.
There is currently a proposal for compile time reflection (AFAIK) and with
it a library function with such functionality should be possible (I'm not
saying that it should be added to the standard library).
> Scoped enumerations require a `static_cast`to cast between
> enumerators("enumeration values") and the value types. However, they
> provide little safety checking, and can return undefined values in case of
> failure.
> I propose a new cast `enumeration_cast` that throws an exception if the
> cast fails(no matching enumerators) instead of returning undefined values.
> Moreover, if the enumerator types do not match or the cast parameter is
> `constexpr`(known at compile time) and the cast cannot succeed, the program
> shall be ill-formed.
>
There is already, in C++23, std::to_underlying
<https://en.cppreference.com/w/cpp/utility/to_underlying> which casts the
enum to the correct underlying type without the need for the user to
specify it explicitly.
About the exception throwing in case of invalid value, I think it will need
some kind of reflection in order to get the possible enumeration values.
There is currently a proposal for compile time reflection (AFAIK) and with
it a library function with such functionality should be possible (I'm not
saying that it should be added to the standard library).
Received on 2022-08-10 12:35:46