xref: /llvm-project/clang/test/CXX/expr/expr.const/p6-2a.cpp (revision 880fa7faa97bad63e403c924263b01fb81783227)
1 // RUN: %clang_cc1 -std=c++2a -verify %s
2 
3 constexpr int non_class = 42;
4 constexpr int arr_non_class[5] = {1, 2, 3};
5 
6 struct A {
7   int member = 1;
~AA8   constexpr ~A() { member = member + 1; }
9 };
10 constexpr A class_ = {};
11 constexpr A arr_class[5] = {{}, {}};
12 
13 struct Mutable {
14   mutable int member = 1; // expected-note {{declared here}}
~MutableMutable15   constexpr ~Mutable() { member = member + 1; } // expected-note {{read of mutable member}}
16 };
17 constexpr Mutable mut_member; // expected-error {{must have constant destruction}} expected-note {{in call}}
18 
19 struct MutableStore {
20   mutable int member = 1; // expected-note {{declared here}}
~MutableStoreMutableStore21   constexpr ~MutableStore() { member = 2; } // expected-note {{assignment to mutable member}}
22 };
23 constexpr MutableStore mut_store; // expected-error {{must have constant destruction}} expected-note {{in call}}
24 
25 // Note: the constant destruction rules disallow this example even though hcm.n is a const object.
26 struct MutableConst {
27   struct HasConstMember {
28     const int n = 4;
29   };
30   mutable HasConstMember hcm; // expected-note {{here}}
~MutableConstMutableConst31   constexpr ~MutableConst() {
32     int q = hcm.n; // expected-note {{read of mutable}}
33   }
34 };
35 constexpr MutableConst mc; // expected-error {{must have constant destruction}} expected-note {{in call}}
36 
37 struct Temporary {
38   int &&temp;
~TemporaryTemporary39   constexpr ~Temporary() {
40     int n = temp; // expected-note {{outside the expression that created the temporary}}
41   }
42 };
43 constexpr Temporary t = {3}; // expected-error {{must have constant destruction}} expected-note {{created here}} expected-note {{in call}}
44 
45 namespace P1073R3 {
f()46 consteval int f() { return 42; } // expected-note 2 {{declared here}}
g()47 consteval auto g() { return f; }
h(int (* p)()=g ())48 consteval int h(int (*p)() = g()) { return p(); }
49 constexpr int r = h();
50 constexpr auto e = g();  // expected-error {{call to consteval function 'P1073R3::g' is not a constant expression}} \
51                             expected-error {{constexpr variable 'e' must be initialized by a constant expression}} \
52                             expected-note 2 {{pointer to a consteval declaration is not a constant expression}}
53 static_assert(r == 42);
54 } // namespace P1073R3
55