| 1 |
// PR c++/88337 - Implement P1327R1: Allow dynamic_cast/typeid in constexpr. |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
// Test non-polymorphic type. |
| 4 |
|
| 5 |
#include <typeinfo> |
| 6 |
|
| 7 |
struct B { |
| 8 |
const std::type_info &ti = typeid (*this); |
| 9 |
}; |
| 10 |
|
| 11 |
struct D : B { }; |
| 12 |
|
| 13 |
constexpr B b; |
| 14 |
constexpr D d; |
| 15 |
|
| 16 |
static_assert (&b.ti == &typeid (B)); |
| 17 |
static_assert (&B{}.ti == &typeid (B)); |
| 18 |
static_assert (&B().ti == &typeid (B)); |
| 19 |
static_assert (&typeid ((B())) == &typeid (B)); |
| 20 |
static_assert (&typeid ((B{})) == &typeid (B)); |
| 21 |
|
| 22 |
static_assert (&d.ti == &typeid (B)); |
| 23 |
static_assert (&D{}.ti == &typeid (B)); |
| 24 |
static_assert (&D().ti == &typeid (B)); |
| 25 |
static_assert (&typeid ((D())) == &typeid (D)); |
| 26 |
static_assert (&typeid ((D{})) == &typeid (D)); |
| 27 |
|
| 28 |
extern D ed; |
| 29 |
static_assert (&typeid (ed) == &typeid (D)); |
| 30 |
|
| 31 |
constexpr const B &r = d; |
| 32 |
static_assert (&typeid (r) == &typeid (B)); |
| 33 |
|
| 34 |
constexpr bool |
| 35 |
test () |
| 36 |
{ |
| 37 |
// If expression is not a glvalue expression of polymorphic type, |
| 38 |
// typeid does not evaluate the expression |
| 39 |
bool ok = true; |
| 40 |
(void) typeid (ok = false, D()); |
| 41 |
(void) typeid (ok = false, B()); |
| 42 |
(void) typeid (ok = false, b); |
| 43 |
(void) typeid (ok = false, d); |
| 44 |
return ok; |
| 45 |
} |
| 46 |
|
| 47 |
static_assert (test ()); |