| 1 |
// P0784R7 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
|
| 4 |
struct S |
| 5 |
{ |
| 6 |
constexpr S () : s (0) {} |
| 7 |
constexpr ~S () {} |
| 8 |
int s; |
| 9 |
}; |
| 10 |
struct T // { dg-message "'T' is not literal because" "" { target { ! implicit_constexpr } } } |
| 11 |
{ // { dg-message "'T' does not have 'constexpr' destructor" "" { target { ! implicit_constexpr } } .-1 } |
| 12 |
constexpr T () : t (0) {} |
| 13 |
~T () {} // { dg-message "defaulted destructor calls non-'constexpr' 'T::~T\\(\\)'" "" { target { ! implicit_constexpr } } } |
| 14 |
int t; |
| 15 |
}; |
| 16 |
struct U : public S |
| 17 |
{ |
| 18 |
constexpr U () : u (0) {} |
| 19 |
constexpr ~U () = default; // { dg-error "explicitly defaulted function 'constexpr U::~U\\(\\)' cannot be declared 'constexpr' because the implicit declaration is not 'constexpr'" "" { target { ! implicit_constexpr } } } |
| 20 |
int u; |
| 21 |
T t; |
| 22 |
}; |
| 23 |
struct V : virtual public S |
| 24 |
{ |
| 25 |
V () : v (0) {} |
| 26 |
constexpr ~V () = default; // { dg-error "explicitly defaulted function 'constexpr V::~V\\(\\)' cannot be declared 'constexpr' because the implicit declaration is not 'constexpr'" } |
| 27 |
int v; |
| 28 |
}; |
| 29 |
struct W0 |
| 30 |
{ |
| 31 |
constexpr W0 () : w (0) {} |
| 32 |
constexpr W0 (int x) : w (x) {} |
| 33 |
constexpr ~W0 () { if (w == 5) asm (""); w = 3; } |
| 34 |
int w; |
| 35 |
}; |
| 36 |
struct W1 |
| 37 |
{ |
| 38 |
constexpr W1 () : w (0) {} |
| 39 |
constexpr W1 (int x) : w (x) {} |
| 40 |
constexpr ~W1 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 41 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 42 |
int w; |
| 43 |
}; |
| 44 |
struct W2 |
| 45 |
{ |
| 46 |
constexpr W2 () : w (0) {} |
| 47 |
constexpr W2 (int x) : w (x) {} |
| 48 |
constexpr ~W2 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 49 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 50 |
int w; |
| 51 |
}; |
| 52 |
struct W3 |
| 53 |
{ |
| 54 |
constexpr W3 () : w (0) {} |
| 55 |
constexpr W3 (int x) : w (x) {} |
| 56 |
constexpr ~W3 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 57 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 58 |
int w; |
| 59 |
}; |
| 60 |
struct W4 |
| 61 |
{ |
| 62 |
constexpr W4 () : w (0) {} |
| 63 |
constexpr W4 (int x) : w (x) {} |
| 64 |
constexpr ~W4 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 65 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 66 |
int w; |
| 67 |
}; |
| 68 |
struct W5 |
| 69 |
{ |
| 70 |
constexpr W5 () : w (0) {} |
| 71 |
constexpr W5 (int x) : w (x) {} |
| 72 |
constexpr ~W5 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 73 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 74 |
int w; |
| 75 |
}; |
| 76 |
struct W6 |
| 77 |
{ |
| 78 |
constexpr W6 () : w (0) {} |
| 79 |
constexpr W6 (int x) : w (x) {} |
| 80 |
constexpr ~W6 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 81 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 82 |
int w; |
| 83 |
}; |
| 84 |
struct W7 |
| 85 |
{ |
| 86 |
constexpr W7 () : w (0) {} |
| 87 |
constexpr W7 (int x) : w (x) {} |
| 88 |
constexpr ~W7 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 89 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 90 |
int w; |
| 91 |
}; |
| 92 |
struct W8 |
| 93 |
{ |
| 94 |
constexpr W8 () : w (0) {} |
| 95 |
constexpr W8 (int x) : w (x) {} |
| 96 |
constexpr ~W8 () { if (w == 5) asm (""); w = 3; } // { dg-error "inline assembly is not a constant expression" } |
| 97 |
// { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 } |
| 98 |
int w; |
| 99 |
}; |
| 100 |
struct X : public T |
| 101 |
{ |
| 102 |
constexpr X () : x (0) {} |
| 103 |
constexpr ~X () = default; // { dg-error "explicitly defaulted function 'constexpr X::~X\\(\\)' cannot be declared 'constexpr' because the implicit declaration is not 'constexpr'" "" { target { ! implicit_constexpr } } } |
| 104 |
int x; |
| 105 |
}; |
| 106 |
constexpr S s; |
| 107 |
constexpr T t; // { dg-error "the type 'const T' of 'constexpr' variable 't' is not literal" "" { target { ! implicit_constexpr } } } |
| 108 |
constexpr W0 w1; |
| 109 |
constexpr W0 w2 = 12; |
| 110 |
constexpr W1 w3 = 5; // { dg-message "in 'constexpr' expansion of" } |
| 111 |
constexpr W0 w4[3] = { 1, 2, 3 }; |
| 112 |
constexpr W2 w5[3] = { 4, 5, 6 }; // { dg-message "in 'constexpr' expansion of" } |
| 113 |
|
| 114 |
void |
| 115 |
f1 () |
| 116 |
{ |
| 117 |
constexpr S s2; |
| 118 |
constexpr W0 w6; |
| 119 |
constexpr W0 w7 = 12; |
| 120 |
constexpr W3 w8 = 5; // { dg-message "in 'constexpr' expansion of" } |
| 121 |
constexpr W0 w9[3] = { 1, 2, 3 }; |
| 122 |
constexpr W4 w10[3] = { 4, 5, 6 }; // { dg-message "in 'constexpr' expansion of" } |
| 123 |
} |
| 124 |
|
| 125 |
constexpr int |
| 126 |
f2 () |
| 127 |
{ |
| 128 |
constexpr S s3; |
| 129 |
constexpr W0 w11; |
| 130 |
constexpr W0 w12 = 12; |
| 131 |
constexpr W5 w13 = 5; // { dg-message "in 'constexpr' expansion of" } |
| 132 |
constexpr W0 w14[3] = { 1, 2, 3 }; |
| 133 |
constexpr W6 w15[3] = { 4, 5, 6 }; // { dg-message "in 'constexpr' expansion of" } |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
constexpr int |
| 138 |
f3 () |
| 139 |
{ |
| 140 |
S s3; |
| 141 |
W0 w11; |
| 142 |
W0 w12 = 12; |
| 143 |
W0 w14[3] = { 1, 2, 3 }; |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
constexpr int x3 = f3 (); |
| 148 |
|
| 149 |
constexpr int |
| 150 |
f4 () |
| 151 |
{ |
| 152 |
W7 w13 = 5; |
| 153 |
return 0; |
| 154 |
} // { dg-message "in 'constexpr' expansion of" } |
| 155 |
|
| 156 |
constexpr int x4 = f4 (); // { dg-message "in 'constexpr' expansion of" } |
| 157 |
|
| 158 |
constexpr int |
| 159 |
f5 () |
| 160 |
{ |
| 161 |
W8 w15[3] = { 4, 5, 6 }; // { dg-message "in 'constexpr' expansion of" } |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
constexpr int x5 = f5 (); // { dg-message "in 'constexpr' expansion of" } |
| 166 |
|
| 167 |
void |
| 168 |
f6 () |
| 169 |
{ |
| 170 |
constexpr T t2; // { dg-error "the type 'const T' of 'constexpr' variable 't2' is not literal" "" { target { ! implicit_constexpr } } } |
| 171 |
} |
| 172 |
|
| 173 |
constexpr int |
| 174 |
f7 () |
| 175 |
{ |
| 176 |
constexpr T t3; // { dg-error "the type 'const T' of 'constexpr' variable 't3' is not literal" "" { target { ! implicit_constexpr } } } |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
constexpr int |
| 181 |
f8 () |
| 182 |
{ |
| 183 |
T t4; // { dg-error "variable 't4' of non-literal type 'T' in 'constexpr' function only available with" "" { target { c++20_down && { ! implicit_constexpr } } } } |
| 184 |
return 0; |
| 185 |
} |