| 1 |
// PR c++/88337 - Implement P1327R1: Allow dynamic_cast/typeid in constexpr. |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
// Test polymorphic type. |
| 4 |
|
| 5 |
#include <typeinfo> |
| 6 |
|
| 7 |
struct B { |
| 8 |
virtual void foo (); |
| 9 |
const std::type_info &ti_base = typeid (*this); |
| 10 |
}; |
| 11 |
|
| 12 |
struct D : B { |
| 13 |
const std::type_info &ti = typeid (*this); |
| 14 |
}; |
| 15 |
|
| 16 |
constexpr B b; |
| 17 |
constexpr D d; |
| 18 |
|
| 19 |
static_assert (&b.ti_base == &typeid (B)); |
| 20 |
static_assert (&B{}.ti_base == &typeid (B)); |
| 21 |
static_assert (&B().ti_base == &typeid (B)); |
| 22 |
static_assert (&typeid ((B())) == &typeid (B)); |
| 23 |
static_assert (&typeid ((B{})) == &typeid (B)); |
| 24 |
|
| 25 |
static_assert (&d.ti == &typeid (D)); |
| 26 |
static_assert (&D{}.ti == &typeid (D)); |
| 27 |
static_assert (&D().ti == &typeid (D)); |
| 28 |
static_assert (&typeid ((D())) == &typeid (D)); |
| 29 |
static_assert (&typeid ((D{})) == &typeid (D)); |
| 30 |
|
| 31 |
extern D ed; |
| 32 |
// ??? Should this succeed? |
| 33 |
static_assert (&typeid (ed) == &typeid (D)); |
| 34 |
|
| 35 |
constexpr const B &r = d; |
| 36 |
static_assert (&typeid (r) == &typeid (D)); |
| 37 |
|
| 38 |
constexpr bool |
| 39 |
test () |
| 40 |
{ |
| 41 |
// If expression is a glvalue expression that identifies an object |
| 42 |
// of a polymorphic type, the typeid expression evaluates the expression. |
| 43 |
bool ok = true; |
| 44 |
// Not a glvalue. |
| 45 |
(void) typeid (ok = false, D()); |
| 46 |
(void) typeid (ok = false, B()); |
| 47 |
if (!ok) |
| 48 |
return false; |
| 49 |
|
| 50 |
// Polymorphic glvalue -- this needs to be evaluated. |
| 51 |
ok = false; |
| 52 |
(void) typeid (ok = true, b); |
| 53 |
if (!ok) |
| 54 |
return false; |
| 55 |
ok = false; |
| 56 |
(void) typeid (ok = true, d); |
| 57 |
return ok; |
| 58 |
} |
| 59 |
|
| 60 |
static_assert (test ()); |