Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/constinit11.C

raw
1 // PR c++/91360 - Implement C++20 P1143R2: constinit
2 // { dg-do compile { target c++20 } }
3
4 int foo ();
5 constexpr int constfoo () { return 42; }
6 int gl = 42;
7
8 struct nonliteral {
9 int m;
10 nonliteral() : m() { }
11 nonliteral(int n) : m(n) { }
12 ~nonliteral() {}
13 };
14
15 struct literal {
16 int m;
17 constexpr literal() : m() { }
18 constexpr literal(int n) : m(n) { }
19 };
20
21 struct pod {
22 int m;
23 };
24
25 struct S {
26 static constinit pod p;
27 static constinit pod pc;
28 static const constinit nonliteral n;
29 };
30
31 struct W {
32 int w = 42;
33 };
34
35 constinit W w;
36
37 constinit const int &r1 = gl;
38 constinit thread_local const int &r2 = gl;
39 constinit const int &r3 // { dg-error "variable .r3. does not have a constant initializer" }
40 = foo (); // { dg-error "call to non-.constexpr. function" }
41 constinit const literal &r4 = 42;
42 constinit const nonliteral &r5 // { dg-error "variable .r5. does not have a constant initializer" "" { target { ! implicit_constexpr } } }
43 = 42; // { dg-error "call to non-.constexpr. function" "" { target { ! implicit_constexpr } } }
44 constinit const int &r6 = nonliteral(2).m; // { dg-error "variable .r6. does not have a constant initializer|call to non-.constexpr. function" "" { target { ! implicit_constexpr } } }
45
46 constinit pod p1;
47 constinit pod p2 = { 42 };
48 constinit pod p3 = { constfoo() };
49 constinit pod p4 = { foo() }; // { dg-error "variable .p4. does not have a constant initializer|call to non-.constexpr. function" }
50
51 constexpr literal lit;
52 constinit literal l1 = lit;
53 constinit literal l2 = 42;
54 constinit literal l3 = constfoo();
55 constinit literal l4 = foo(); // { dg-error "variable .l4. does not have a constant initializer|call to non-.constexpr. function" }
56 constinit literal l5 = {};
57 constinit literal l6{};
58 constinit thread_local literal l7 = lit;
59 constinit thread_local literal l8 = 42;
60 constinit thread_local literal l9 = constfoo();
61 constinit thread_local literal l10 = foo(); // { dg-error "variable .l10. does not have a constant initializer|call to non-.constexpr. function" }
62 constinit thread_local literal l11{};
63
64 pod S::p;
65 constinit pod S::pc(S::p); // { dg-error "variable .S::pc. does not have a constant initializer|not usable" }
66
67 constinit const nonliteral S::n(42); // { dg-error "variable .S::n. does not have a constant initializer|call to non-.constexpr. function" "" { target { ! implicit_constexpr } } }
68 constinit int n1 = nonliteral{42}.m; // { dg-error "variable .n1. does not have a constant initializer|temporary of non-literal type" "" { target { ! implicit_constexpr } } }
69 constinit int n2 = literal{42}.m;
70
71 void
72 fn1 ()
73 {
74 const int c = 42;
75 static constinit const int &l // { dg-error "variable .l. does not have a constant initializer" }
76 = c; // { dg-error "not a constant" }
77 static const int &l2 = 10;
78 static const int &l3 = gl;
79 }