1 // PR c++/88337 - Implement P1327R1: Allow dynamic_cast/typeid in constexpr.
2 // { dg-do compile { target c++2a } }
3 // { dg-additional-options "-fdelete-null-pointer-checks" }
4 
5 // dynamic_cast in a constructor.
6 // [class.cdtor]#6: "If the operand of the dynamic_cast refers to the object
7 // under construction or destruction and the static type of the operand is not
8 // a pointer to or object of the constructor or destructor's own class or one
9 // of its bases, the dynamic_cast results in undefined behavior.
10 
11 struct V {
12   virtual void f();
13 };
14 
15 struct A : V { };
16 
17 struct B : V {
18   constexpr B(V*, A*);
19 };
20 
21 struct D : A, B {
DD22   constexpr D() : B((A*)this, this) { } // { dg-message "in 'constexpr' expansion of" }
23 };
24 
B(V * v,A * a)25 constexpr B::B(V* v, A* a)
26 {
27   // well-defined: v of type V*, V base of B results in B*
28   B* b = dynamic_cast<B*>(v);
29   if (b != nullptr)
30     __builtin_abort ();
31 
32   B& br = dynamic_cast<B&>(*v); // { dg-error "reference .dynamic_cast. failed" }
33 // { dg-message "dynamic type .A. of its operand does not have an unambiguous public base class .B." "" { target *-*-* } .-1 }
34 }
35 
36 constexpr D d; // { dg-message "in 'constexpr' expansion of" }
37