Re:Enum class improvements for C++17, C++20 and C++23
by josefx on Monday August 05, 2024 @03:47AM
(#41158974)
There is a difference between an API promissing that a value wont be null and a buggy program setting a null where it should not. A reference is only null if someone fucked up. As a programmer you can usually rely on a reference not being null and you couldn't do anything about it if it was anyway within the constraints of the language.
[ Parent
| on HN ]
Re:Enum class improvements for C++17, C++20 and C++23
by TeMPOraL on Monday August 05, 2024 @04:50AM
(#41159288)
In the same way, an `enum class` variable's value being outside of the set defined in the `enum class` is also a fuckup.
Re:Enum class improvements for C++17, C++20 and C++23
by gpderetta on Monday August 05, 2024 @05:44AM
(#41159553)
no, deferencing a null ptr is UB. An enum class outside the declared values is perfectly valid.
You could design a language feature where integer to enum is checked, but that's not enum.
Enum classes already add scoping, forbid implicit conversions and allow explicit underlying types. Those are pure extensions. Making undeclared values invalid or UB would be very surprising to people used to normal enums.