CWG1766 made out-of-range cast to enumeration without fixed underlying
type raise undefined behavior. Such UB arguably also applies to
`bit_cast`, although it's not required that UB in `bit_cast` makes the
expression non-constant.
Currently, only Clang has implemented CWG1766, while Clang's underlying
`__builtin_bit_cast` happens to be a workaround. However, it's more
reliable to me to rely on the guarantee that core language UB causes
constant evaluation failure.
The approach in this patch effectively detects whether
`std::integral_constant<E, static_cast<E>(V)>` is a valid type, which is
equivalent to whether `static_cast<E>(V)` is a constant expression. When
the answer is `false`, value `V` can't be an enumerator of `E`.
The comments in the code regarding MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX values were not updated according to the latest changes on those default values.
It might make no sense, but on MSVC it can generate a compile-time
error, especially if an enumerator's value is out of range.
Example:
error C3615: constexpr function 'magic_enum::detail::names' cannot
result in a constant expression
...
note: failure was caused by call of undefined function or one not
declared 'constexpr'
...
note: see usage of '__builtin_array_init_helper'
* fixes 'std::ptrdiff_t' to 'std::size_t' casting error
addresses this compiler warning:
```text
[..]/magic_enum_utility.hpp:101:31: warning: conversion to ‘std::size_t’ {aka ‘long unsigned int’} from ‘std::ptrdiff_t’ {aka ‘long int’} may change the sign of the result [-Wsign-conversion]
101 | return enum_value<D, S>(index);
| ^~~~~
```
* suppresses `-Wuseless-cast` for static_cast<char_type>('\0')
needed in case 'char_type' is 'char' (common on Linux but rare on Windows?)
```text
[..]/magic_enum.hpp:275:114: warning: useless cast to type ‘using magic_enum::char_type = using std::basic_string_view<char>::value_type = char’ {aka ‘char’} [-Wuseless-cast]
275 | constexpr static_str(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., static_cast<char_type>('\0')} {}
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
```