Debian Codegraph @

gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime7.C

raw
1 // PR c++/71093
2 // { dg-do compile { target c++14 } }
3
4 constexpr int f (const int *p)
5 {
6 typedef int T;
7 p->~T (); // { dg-error "destroying" }
8 return *p;
9 }
10
11 constexpr int i = 0;
12 constexpr int j = f (&i);
13
14
15 template <typename T>
16 constexpr bool test_access() {
17 T x {};
18 x.~T();
19 T y = x; // { dg-error "lifetime" }
20 return true;
21 }
22
23 template <typename T>
24 constexpr bool test_modification() {
25 T x {};
26 x.~T();
27 x = T(); // { dg-error "lifetime" }
28 return true;
29 }
30
31 template <typename T>
32 constexpr bool test_scope() {
33 {
34 T x {};
35 x.~T();
36 } // { dg-error "destroying" }
37 return true;
38 }
39
40 template <typename T>
41 constexpr bool test_destroy_temp() {
42 T{}.~T(); // { dg-error "destroying" }
43 return true;
44 }
45
46 template <typename T>
47 constexpr bool test_parameter(T t) {
48 // note: error message occurs at point of call
49 t.~T();
50 return true;
51 }
52
53 template <typename T>
54 constexpr void test_bindings_impl(int n) {
55 if (n == 0) return;
56 T a {};
57 if (n == 1) return;
58 T b {};
59 }
60
61 template <typename T>
62 constexpr bool test_bindings() {
63 test_bindings_impl<T>(1);
64 test_bindings_impl<T>(0);
65 test_bindings_impl<T>(2);
66 return true;
67 }
68
69 constexpr bool i1 = test_access<int>(); // { dg-message "in .constexpr." }
70 constexpr bool i2 = test_modification<int>(); // { dg-message "in .constexpr." }
71 constexpr bool i3 = test_scope<int>(); // { dg-message "in .constexpr." }
72 constexpr bool i4 = test_destroy_temp<int>(); // { dg-message "in .constexpr." "" { xfail *-*-* } }
73 constexpr bool i5 = test_parameter(int{}); // { dg-error "destroying" }
74 constexpr bool i6 = test_bindings<int>();
75
76 struct Trivial { int x; };
77 constexpr bool t1 = test_access<Trivial>(); // { dg-message "in .constexpr." }
78 constexpr bool t2 = test_modification<Trivial>(); // { dg-message "in .constexpr." }
79 constexpr bool t3 = test_scope<Trivial>(); // { dg-message "in .constexpr." }
80 constexpr bool t4 = test_destroy_temp<Trivial>(); // { dg-message "in .constexpr." }
81 constexpr bool t5 = test_parameter(Trivial{}); // { dg-error "destroying" }
82 constexpr bool t6 = test_bindings<Trivial>();
83
84 #if __cplusplus >= 202002L
85 struct NonTrivial { int x; constexpr ~NonTrivial() {} }; // { dg-error "destroying" "" { target c++20 } }
86 constexpr bool n1 = test_access<NonTrivial>(); // { dg-message "in .constexpr." "" { target c++20 } }
87 constexpr bool n2 = test_modification<NonTrivial>(); // { dg-message "in .constexpr." "" { target c++20 } }
88 constexpr bool n3 = test_scope<NonTrivial>(); // { dg-message "in .constexpr." "" { target c++20 } }
89 constexpr bool n4 = test_destroy_temp<NonTrivial>(); // { dg-message "in .constexpr." "" { target c++20 } }
90 constexpr bool n5 = test_parameter(NonTrivial{}); // { dg-message "in .constexpr." "" { target c++20 } }
91 constexpr bool n6 = test_bindings<NonTrivial>();
92 #endif
93