Debian Codegraph @

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

raw
1 // PR c++/91360 - Implement C++20 P1143R2: constinit
2 // { dg-do compile { target c++20 } }
3 // Test basic usage of 'constinit'.
4
5 const char *g() { return "dynamic init"; }
6 constexpr const char *f(bool p) { return p ? "constant init" : g(); } // { dg-error "call to non-.constexpr. function" }
7
8 constinit const char *c = f(true);
9 constinit const char *d = f(false); // { dg-error "variable .d. does not have a constant initializer" }
10 // { dg-message "in .constexpr. expansion of" "" { target *-*-* } .-1 }
11 static constinit const char *e = f(true);
12
13 constexpr int foo(int x) { return x; }
14 constinit int i = foo(42);
15 constinit int j // { dg-error "variable .j. does not have a constant initializer" }
16 = foo(i); // { dg-error "not usable in a constant expression" }
17
18 int y = 42;
19 constinit int x // { dg-error "variable .x. does not have a constant initializer" }
20 = y; // { dg-error "not usable in a constant expression" }
21
22 constinit int z;
23 const constinit unsigned cst = 1u;
24
25 void
26 fn ()
27 {
28 static constinit int m = foo(42);
29 static constinit int n // { dg-error "variable .n. does not have a constant initializer" }
30 = foo(m); // { dg-error "not usable in a constant expression" }
31
32 // Make sure we can still modify constinit variables.
33 c = "foo";
34 i = 10;
35 m = 90;
36 // ... unless they're 'const'.
37 cst *= 2; // { dg-error "assignment of read-only variable" }
38 }