Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/consteval-virtual4.C

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