Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic17.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 // 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 // See <https://gcc.gnu.org/ml/gcc-patches/2019-12/msg01521.html>.
11
12 struct V {
13 virtual void f();
14 };
15
16 struct A : V { };
17
18 struct B : V {
19 constexpr B(V*, A*);
20 };
21
22 struct D : B, A {
23 constexpr D() : B((A*)this, this) { } // { dg-message "in 'constexpr' expansion of" }
24 };
25
26 constexpr B::B(V* v, A* a)
27 {
28 dynamic_cast<B*>(a); // { dg-error "accessing uninitialized member" }
29 }
30
31 constexpr D d; // { dg-message "in 'constexpr' expansion of" }