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