| 1 |
// PR c++/88337 - Implement P1327R1: Allow dynamic_cast/typeid in constexpr. |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
|
| 4 |
// Downcast. |
| 5 |
|
| 6 |
struct B { |
| 7 |
virtual void baz () {} |
| 8 |
}; |
| 9 |
|
| 10 |
struct D : B { }; |
| 11 |
|
| 12 |
constexpr bool |
| 13 |
fn () |
| 14 |
{ |
| 15 |
bool ok = true; |
| 16 |
B b; |
| 17 |
B *b1 = &b; |
| 18 |
if (D *pd = dynamic_cast<D*>(b1)) |
| 19 |
ok = false; |
| 20 |
|
| 21 |
D d; |
| 22 |
B *b2 = &d; |
| 23 |
if (D *pd = dynamic_cast<D*>(b2)) |
| 24 |
/*OK*/; |
| 25 |
else |
| 26 |
ok = false; |
| 27 |
|
| 28 |
return ok; |
| 29 |
} |
| 30 |
|
| 31 |
static_assert(fn ()); |
| 32 |
|
| 33 |
constexpr D d; |
| 34 |
constexpr B b; |
| 35 |
constexpr B *b1 = const_cast<B*>(&b); |
| 36 |
constexpr B *b2 = const_cast<D*>(&d); |
| 37 |
static_assert(dynamic_cast<D*>(b2) == &d); |
| 38 |
static_assert(&dynamic_cast<D&>(*b2) == &d); |
| 39 |
static_assert(dynamic_cast<const B*>(&d) == &d); |
| 40 |
static_assert(&dynamic_cast<const B&>(d) == &d); |