<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/120211>120211</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`__builtin_bit_cast` to `bool` diverges from GCC's behavior
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend,
constexpr
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tbaederr
</td>
</tr>
</table>
<pre>
This is the specific case:
```c++
constexpr bool A = __builtin_bit_cast(bool, 'A');
static_assert(A);
```
GCC happily compiles this while clang prints an error:
```
array.cpp:128:16: error: constexpr variable 'a' must be initialized by a constant expression
128 | constexpr bool a = __builtin_bit_cast(bool, 'A');
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
array.cpp:128:20: note: value 65 cannot be represented in type 'bool'
128 | constexpr bool a = __builtin_bit_cast(bool, 'A');
| ^
```
This specific diagnostic is tested exactly _once_, in `SemaCXX/constexpr-builtin-bit-cast.cpp`. It's an edge case which clang checks for all integral types though:
https://github.com/llvm/llvm-project/blob/0693b9e9ccdec5f09a3080b1bec73f5004a8dfa3/clang/lib/AST/ExprConstant.cpp#L7495-L7507
I think the relevant part from https://eel.is/c++draft/bit.cast is
> For the result and each object created within it, if there is no value of the object's type corresponding to the value representation produced, the behavior is undefined[.](https://eel.is/c++draft/bit.cast#2.sentence-5)
If there are multiple such values, which value is produced is unspecified[.](https://eel.is/c++draft/bit.cast#2.sentence-6)
Does the first sentence apply here or the second?
CC @zygoloid
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8VU2P2zYQ_TX0ZWCDoqwPH3RQvHGwQG7JITeDokbSNDQpkJSzzqG_vSDlddBtUGCBooRgC-R8vDfzhpLe02gQG1Z8YMXTRi5hsq4JncQendt0tr81XyfyQB7ChOBnVDSQAiU9srxlvGUlXx_FxIf48FZZ4wO-zA46azW0wPInOJ-7hXQgc-4onJX0gYk6njNxB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