Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic4.C

raw
1 // PR c++/88337 - Implement P1327R1: Allow dynamic_cast/typeid in constexpr.
2 // { dg-do compile { target c++20 } }
3 // { dg-additional-options "-fdelete-null-pointer-checks" }
4
5 // From clang's constant-expression-cxx2a.cpp.
6
7 struct A2 { virtual void a2(); };
8 struct A : A2 { virtual void a(); };
9 struct B : A {};
10 struct C2 { virtual void c2(); };
11 struct C : A, C2 { A *c = dynamic_cast<A*>(static_cast<C2*>(this)); };
12 struct D { virtual void d(); };
13 struct E { virtual void e(); };
14 struct F : B, C, D, private E { void *f = dynamic_cast<void*>(static_cast<D*>(this)); };
15 struct Padding { virtual void padding(); };
16 struct G : Padding, F {};
17
18 constexpr G g;
19
20 // During construction of C, A is unambiguous subobject of dynamic type C.
21 static_assert(g.c == (C*)&g);
22 // ... but in the complete object, the same is not true, so the runtime fails.
23 static_assert(dynamic_cast<const A*>(static_cast<const C2*>(&g)) == nullptr);
24
25 // dynamic_cast<void*> produces a pointer to the object of the dynamic type.
26 static_assert(g.f == (void*)(F*)&g);
27 static_assert(dynamic_cast<const void*>(static_cast<const D*>(&g)) == &g);
28
29 constexpr int d_a = (dynamic_cast<const A&>(static_cast<const D&>(g)), 0); // { dg-error "reference .dynamic_cast. failed" }
30 // { dg-message ".A. is an ambiguous base class of dynamic type .G." "" { target *-*-* } .-1 }
31
32 // Can navigate from A2 to its A...
33 static_assert(&dynamic_cast<A&>((A2&)(B&)g) == &(A&)(B&)g);
34 // ... and from B to its A ...
35 static_assert(&dynamic_cast<A&>((B&)g) == &(A&)(B&)g);
36 // ... but not from D.
37 static_assert(&dynamic_cast<A&>((D&)g) == &(A&)(B&)g); // { dg-error "non-constant condition for static assertion|reference .dynamic_cast. failed" }
38 // { dg-message ".A. is an ambiguous base class of dynamic type .G." "" { target *-*-* } .-1 }
39
40 // Can cast from A2 to sibling class D.
41 static_assert(&dynamic_cast<D&>((A2&)(B&)g) == &(D&)g);
42
43 // Cannot cast from private base E to derived class F.
44 constexpr int e_f = (dynamic_cast<F&>((E&)g), 0); // { dg-error "reference .dynamic_cast. failed" }
45 // { dg-message "static type .const E. of its operand is a non-public base class of dynamic type .G." "" { target *-*-* } .-1 }
46
47 // Cannot cast from B to private sibling E.
48 constexpr int b_e = (dynamic_cast<E&>((B&)g), 0); // { dg-error "reference .dynamic_cast. failed" }
49 // { dg-message "dynamic type .G. of its operand does not have an unambiguous public base class .E." "" { target *-*-* } .-1 }
50
51 struct Unrelated { virtual void unrelated(); };
52
53 constexpr int b_unrelated = (dynamic_cast<Unrelated&>((B&)g), 0); // { dg-error "reference .dynamic_cast. failed" }
54 // { dg-message "dynamic type .G. of its operand does not have an unambiguous public base class .Unrelated." "" { target *-*-* } .-1 }
55 constexpr int e_unrelated = (dynamic_cast<Unrelated&>((E&)g), 0); // { dg-error "reference .dynamic_cast. failed" }
56 // { dg-message "static type .const E. of its operand is a non-public base class of dynamic type .G." "" { target *-*-* } .-1 }