| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
|
| 3 |
struct S { |
| 4 |
constexpr S () : s (0) {} |
| 5 |
virtual int foo () const { return 42; } |
| 6 |
consteval virtual int bar () const { return 43; } |
| 7 |
consteval virtual int baz () const { return 44; } |
| 8 |
consteval virtual int qux () const { return 47; } |
| 9 |
int s; |
| 10 |
}; |
| 11 |
struct T : public S { |
| 12 |
constexpr T () : t (0) {} |
| 13 |
consteval int bar () const { return 45; } |
| 14 |
consteval virtual int baz () const { return 46; } |
| 15 |
consteval virtual int grault () const { return 48; } |
| 16 |
int t; |
| 17 |
}; |
| 18 |
|
| 19 |
consteval int |
| 20 |
foo () |
| 21 |
{ |
| 22 |
S s; |
| 23 |
T t; |
| 24 |
S *u = (S *) &t; |
| 25 |
T *v = &t; |
| 26 |
if (s.bar () != 43) throw 1; |
| 27 |
if (s.baz () != 44) throw 2; |
| 28 |
if (t.bar () != 45) throw 3; |
| 29 |
if (t.baz () != 46) throw 4; |
| 30 |
if (u->bar () != 45) throw 5; |
| 31 |
if (u->baz () != 46) throw 6; |
| 32 |
if (s.qux () != 47) throw 7; |
| 33 |
if (t.qux () != 47) throw 8; |
| 34 |
if (u->qux () != 47) throw 9; |
| 35 |
if (v->qux () != 47) throw 10; |
| 36 |
if (v->grault () != 48) throw 11; |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
constexpr S s; |
| 41 |
constexpr T t; |
| 42 |
|
| 43 |
constexpr const S * |
| 44 |
bar (bool x) |
| 45 |
{ |
| 46 |
return x ? &s : (const S *) &t; |
| 47 |
} |
| 48 |
|
| 49 |
int a = foo (); |
| 50 |
int b = bar (false)->bar (); |
| 51 |
int c = bar (true)->baz (); |
| 52 |
static_assert (bar (false)->bar () == 45); |
| 53 |
static_assert (bar (true)->baz () == 44); |