SIGN IN SIGN UP

More reliable value validness detection towards CWG1766 (#398)

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`.
A
A. Jiang committed
2ec43969d87016703aa0acbaff06cc4d8018a815
Parent: 1a1824d
Committed by GitHub <noreply@github.com> on 2/20/2025, 8:34:09 AM