<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/68199>68199</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
constexpr dynamic_cast failure
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
gbMattN
</td>
</tr>
</table>
<pre>
As of c++20, dynamic_cast expressions can appear in constexpr expressions, but the following code
```
struct Base {
virtual int foo() {
return 1;
}
};
struct Derived : public Base {};
Derived d;
int test_it (void) {
constexpr auto const x= (dynamic_cast<void *>(&d)!=0);
return x;
}
int main(void) {
return 0;
}
```
Gives the error
```
<source>:11:26: error: constexpr variable 'x' must be initialized by a constant expression
11 | constexpr auto const x= (dynamic_cast<void *>(&d)!=0);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:11:30: note: dynamic_cast applied to object 'd' whose dynamic type is not constant
11 | constexpr auto const x= (dynamic_cast<void *>(&d)!=0);
| ^
1 error generated.
```
This was built on Ubuntu 22.04 using the main branch of llvm at time of reporting (75f295c2455131993f9c945320736b84c5fcf99c) with the -stdc++20 flag.
It builds successfully on MSVC since v19.36 and GCC since 10.1
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VEmP6zYM_jXMhZjAlrzEBx-yTB56eL10uQ5kmU5UKJKhJZn00N9eyMlkKdLe-oQgsiRS_PjxE4X3ameIWihXUG5mIoa9de2u-y5C-HnW2f7cLj3aASWwFbAVy4CtsT8bcVDyQwofkD5HR94razxKYVCMIwmHyqC0xod0_GiT_LsYMOwJB6u1PSmzQ2l7gmwD2RKq7Pqblj64KAOuhCeEenXZREQ8Khei0KhMwMFaYAtgzbNJGo5CdAZz4A8HUG-uwerN7eAaaUNOHalH4EscY6eVvAd_sL78fxn3_9hPoAL58KECAlscreqf0d2pETHYyxI_gW-S-SO9wNfJG4Etgb9PWVbpLmA58E2Wvh4zu6b7ecdzS_UG7CCUeQnq7p-98H-uyzd1JD8VkZyz7qUN8LW30UlKyPkyz4EvWZWIvfjwRx6OwinRaUJg9SewGg_RB-wIlVFBCa3-pB67M4qLjzCPyrslkOcI9Xqq_P9FMd4ivBxQvuNf_zX-nRueJUqMDZTmp0cmxlEr6jFYtN0fJJOs6j7RdNpbT1_GGM4jofLpkhtPP54cKN8vW_ml0rgjQ04E6ucvhfLrXnk8CY9dVDqgNfhbF02IyNg8KzD61CKS1pJ0sXPCyH1qSlofDygCBnWgtHY0WheSMbBFXQ6sKSUryjLnedPwoZFNUXKW1bzqFoUsBzk0jUxv4KTCfgrw5kN_a3U4aLGbPz6fn8IEsffoo5Tk_RC1PifA33_5fY1eGUl4zJs5r1CYHr-tvzbzbJ7P-pb3DW_EjNq8aqoiL3nJZvtWFqJqFnVT8Y6aQRbEKB8qysSirLpFtZiplmWM51lW5BlrcjZvyrIXXcdZ1WdDLQUUGR2E0vNEydy63Ux5H6mtFnnTzLToSPupyTNm6ITTITCWer5rk89bF3ceikwrH_z9lqCCpvaulidRDkLp6GgWnW73IYwe-BLYFth2p8I-dnNpD8C26bLr9DY6m9QLbDtB8MC2E8S_AwAA__-kUfKM">