Debian Codegraph @

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

raw
1 // PR c++/94034
2 // { dg-do compile { target c++14 } }
3
4 struct A
5 {
6 A() = default; A(const A&);
7 A *p = this;
8 int n = 2;
9 int m = p->n++;
10 };
11
12 constexpr A
13 foo()
14 {
15 return {};
16 }
17
18 constexpr A
19 bar()
20 {
21 A a = foo();
22 a.p->n = 5;
23 return a; // { dg-error "non-.constexpr." "" { target c++20_down } }
24 }
25
26 constexpr int
27 baz()
28 {
29 A b = foo();
30 b.p->n = 10;
31 A c = foo();
32 if (c.p->n != 3 || c.p->m != 2)
33 __builtin_abort();
34 foo();
35 return 0;
36 }
37
38 static_assert(baz() == 0, "");
39
40 constexpr int
41 quux()
42 {
43 const A d = foo();
44 d.p->n++; // { dg-error "const object" }
45 return 0;
46 }
47
48 static_assert(quux() == 0, ""); // { dg-error "non-constant" }