Skip to content

Conversation

@pull
Copy link

@pull pull bot commented Feb 20, 2025

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.1)

Can you help keep this open source service alive? 💖 Please sponsor : )

frederick-vs-ja and others added 4 commits February 20, 2025 12:34
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`.
)

Using `static constexpr` in a header file does not seem to be correct - see [1] - and generates a bug when building the magic_enum module with GCC 15

[1] https://isocpp.org/blog/2018/05/quick-q-use-of-constexpr-in-header-file
@pull pull bot added the ⤵️ pull label Feb 20, 2025
@pull pull bot merged commit a413fcc into Mu-L:master Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants