1 // RUN: %clang_cc1 -std=c++1z -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu
2
3 namespace BaseClassAggregateInit {
4 struct A {
5 int a, b, c;
ABaseClassAggregateInit::A6 constexpr A(int n) : a(n), b(3 * n), c(b - 1) {} // expected-note {{outside the range of representable}}
ABaseClassAggregateInit::A7 constexpr A() : A(10) {};
8 };
9 struct B : A {};
10 struct C { int q; };
11 struct D : B, C { int k; };
12
13 constexpr D d1 = { 1, 2, 3 };
14 static_assert(d1.a == 1 && d1.b == 3 && d1.c == 2 && d1.q == 2 && d1.k == 3);
15
16 constexpr D d2 = { 14 };
17 static_assert(d2.a == 14 && d2.b == 42 && d2.c == 41 && d2.q == 0 && d2.k == 0);
18
19 constexpr D d3 = { A(5), C{2}, 1 };
20 static_assert(d3.a == 5 && d3.b == 15 && d3.c == 14 && d3.q == 2 && d3.k == 1);
21
22 constexpr D d4 = {};
23 static_assert(d4.a == 10 && d4.b == 30 && d4.c == 29 && d4.q == 0 && d4.k == 0);
24
25 constexpr D d5 = { __INT_MAX__ }; // expected-error {{must be initialized by a constant expression}}
26 // expected-note-re@-1 {{in call to 'A({{.*}})'}}
27 }
28
29 namespace NoexceptFunctionTypes {
f()30 template<typename T> constexpr bool f() noexcept(true) { return true; }
31 constexpr bool (*fp)() = f<int>;
32 static_assert(f<int>());
33 static_assert(fp());
34
35 template<typename T> struct A {
fNoexceptFunctionTypes::A36 constexpr bool f() noexcept(true) { return true; }
gNoexceptFunctionTypes::A37 constexpr bool g() { return f(); }
operator ()NoexceptFunctionTypes::A38 constexpr bool operator()() const noexcept(true) { return true; }
39 };
40 static_assert(A<int>().f());
41 static_assert(A<int>().g());
42 static_assert(A<int>()());
43 }
44
45 namespace Cxx17CD_NB_GB19 {
46 const int &r = 0;
47 constexpr int n = r;
48 }
49
50 namespace PR37585 {
51 template <class T> struct S { static constexpr bool value = true; };
f()52 template <class T> constexpr bool f() { return true; }
53 template <class T> constexpr bool v = true;
54
test()55 void test() {
56 if constexpr (true) {}
57 else if constexpr (f<int>()) {}
58 else if constexpr (S<int>::value) {}
59 else if constexpr (v<int>) {}
60 }
61 }
62
63 // Check that assignment operators evaluate their operands right-to-left.
64 namespace EvalOrder {
65 template<typename T> struct lvalue {
66 T t;
getEvalOrder::lvalue67 constexpr T &get() { return t; }
68 };
69
70 struct UserDefined {
71 int n = 0;
operator =EvalOrder::UserDefined72 constexpr UserDefined &operator=(const UserDefined&) { return *this; }
operator +=EvalOrder::UserDefined73 constexpr UserDefined &operator+=(const UserDefined&) { return *this; }
operator <<EvalOrder::UserDefined74 constexpr void operator<<(const UserDefined&) const {}
operator >>EvalOrder::UserDefined75 constexpr void operator>>(const UserDefined&) const {}
operator +EvalOrder::UserDefined76 constexpr void operator+(const UserDefined&) const {}
operator []EvalOrder::UserDefined77 constexpr void operator[](int) const {}
78 };
79 constexpr UserDefined ud;
80
81 struct NonMember {};
operator +=(NonMember,NonMember)82 constexpr void operator+=(NonMember, NonMember) {}
operator <<(NonMember,NonMember)83 constexpr void operator<<(NonMember, NonMember) {}
operator >>(NonMember,NonMember)84 constexpr void operator>>(NonMember, NonMember) {}
operator +(NonMember,NonMember)85 constexpr void operator+(NonMember, NonMember) {}
86 constexpr NonMember nm;
87
f(...)88 constexpr void f(...) {}
89
90 // Helper to ensure that 'a' is evaluated before 'b'.
91 struct seq_checker {
92 bool done_a = false;
93 bool done_b = false;
94
aEvalOrder::seq_checker95 template <typename T> constexpr T &&a(T &&v) {
96 done_a = true;
97 return (T &&)v;
98 }
bEvalOrder::seq_checker99 template <typename T> constexpr T &&b(T &&v) {
100 if (!done_a)
101 throw "wrong";
102 done_b = true;
103 return (T &&)v;
104 }
105
okEvalOrder::seq_checker106 constexpr bool ok() { return done_a && done_b; }
107 };
108
109 // SEQ(expr), where part of the expression is tagged A(...) and part is
110 // tagged B(...), checks that A is evaluated before B.
111 #define A sc.a
112 #define B sc.b
113 #define SEQ(...) static_assert([](seq_checker sc) { void(__VA_ARGS__); return sc.ok(); }({}))
114
115 // Longstanding sequencing rules.
116 SEQ((A(1), B(2)));
117 SEQ((A(true) ? B(2) : throw "huh?"));
118 SEQ((A(false) ? throw "huh?" : B(2)));
119 SEQ(A(true) && B(true));
120 SEQ(A(false) || B(true));
121
122 // From P0145R3:
123
124 // Rules 1 and 2 have no effect ('b' is not an expression).
125
126 // Rule 3: a->*b
127 SEQ(A(ud).*B(&UserDefined::n));
128 SEQ(A(&ud)->*B(&UserDefined::n));
129
130 // Rule 4: a(b1, b2, b3)
131 SEQ(A(f)(B(1), B(2), B(3)));
132
133 // Rule 5: b = a, b @= a
134 SEQ(B(lvalue<int>().get()) = A(0));
135 SEQ(B(lvalue<UserDefined>().get()) = A(ud));
136 SEQ(B(lvalue<int>().get()) += A(0));
137 SEQ(B(lvalue<UserDefined>().get()) += A(ud));
138 SEQ(B(lvalue<NonMember>().get()) += A(nm));
139
140 // Rule 6: a[b]
141 constexpr int arr[3] = {};
142 SEQ(A(arr)[B(0)]);
143 SEQ(A(+arr)[B(0)]);
144 SEQ(A(0)[B(arr)]);
145 SEQ(A(0)[B(+arr)]);
146 SEQ(A(ud)[B(0)]);
147
148 // Rule 7: a << b
149 SEQ(A(1) << B(2));
150 SEQ(A(ud) << B(ud));
151 SEQ(A(nm) << B(nm));
152
153 // Rule 8: a >> b
154 SEQ(A(1) >> B(2));
155 SEQ(A(ud) >> B(ud));
156 SEQ(A(nm) >> B(nm));
157
158 // No particular order of evaluation is specified in other cases, but we in
159 // practice evaluate left-to-right.
160 // FIXME: Technically we're expected to check for undefined behavior due to
161 // unsequenced read and modification and treat it as non-constant due to UB.
162 SEQ(A(1) + B(2));
163 SEQ(A(ud) + B(ud));
164 SEQ(A(nm) + B(nm));
165 SEQ(f(A(1), B(2)));
166
167 #undef SEQ
168 #undef A
169 #undef B
170 }
171
172 namespace LambdaCallOp {
__anon6fb2fa560102null173 constexpr void get_lambda(void (*&p)()) { p = []{}; }
call_lambda()174 constexpr void call_lambda() {
175 void (*p)() = nullptr;
176 get_lambda(p);
177 p();
178 }
179 }
180