1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
2 // RUN: %clang_cc1 -verify=ref %s
5 constexpr void assert(bool C
) {
8 // Invalid in constexpr.
9 (void)(1 / 0); // expected-warning {{undefined}} \
10 // ref-warning {{undefined}}
14 constexpr float f
= 1.0f
;
15 static_assert(f
== 1.0f
, "");
17 constexpr float f2
= 1u * f
;
18 static_assert(f2
== 1.0f
, "");
20 constexpr float f3
= 1.5;
21 constexpr int i3
= f3
;
22 static_assert(i3
== 1, "");
24 constexpr bool b3
= f3
;
25 static_assert(b3
, "");
28 static_assert(1.0f
+ 3u == 4, "");
29 static_assert(4.0f
/ 1.0f
== 4, "");
30 static_assert(10.0f
* false == 0, "");
32 constexpr float floats
[] = {1.0f
, 2.0f
, 3.0f
, 4.0f
};
34 constexpr float m
= 5.0f
/ 0.0f
; // ref-error {{must be initialized by a constant expression}} \
35 // ref-note {{division by zero}} \
36 // expected-error {{must be initialized by a constant expression}} \
37 // expected-note {{division by zero}}
39 static_assert(~2.0f
== 3, ""); // ref-error {{invalid argument type 'float' to unary expression}} \
40 // expected-error {{invalid argument type 'float' to unary expression}}
43 typedef int tdb
[(long long)4e20
]; //expected-error {{variable length}} \
44 //ref-error {{variable length}}
46 /// Initialized by a double.
47 constexpr float df
= 0.0;
48 /// The other way around.
49 constexpr double fd
= 0.0f
;
51 static_assert(0.0f
== -0.0f
, "");
53 const int k
= 3 * (1.0f
/ 3.0f
);
54 static_assert(k
== 1, "");
56 constexpr bool b
= 1.0;
59 constexpr double db
= true;
60 static_assert(db
== 1.0, "");
62 constexpr float fa
[] = {1.0f
, 2.0, 1, false};
63 constexpr double da
[] = {1.0f
, 2.0, 1, false};
65 constexpr float fm
= __FLT_MAX__
;
66 constexpr int someInt
= fm
; // ref-error {{must be initialized by a constant expression}} \
67 // ref-note {{is outside the range of representable values}} \
68 // expected-error {{must be initialized by a constant expression}} \
69 // expected-note {{is outside the range of representable values}}
72 constexpr float f1() {
85 static_assert(f1() == 2, "");
87 constexpr float f2() {
88 float f
= __FLT_MAX__
;
92 static_assert(f2() == __FLT_MAX__
, "");
94 constexpr float ff() {
98 // RHS should be evaluated before LHS, so this should
101 #if __cplusplus <= 201402L
102 // expected-warning@-2 {{multiple unsequenced modifications}} \
103 // ref-warning@-2 {{multiple unsequenced modifications}}
108 static_assert(ff() == 3, "");
110 constexpr float intPlusDouble() {
116 static_assert(intPlusDouble() == 2, "");
118 constexpr double doublePlusInt() {
124 static_assert(doublePlusInt() == 2, "");
126 constexpr float boolPlusDouble() {
132 static_assert(boolPlusDouble(), "");
134 constexpr bool doublePlusbool() {
140 static_assert(doublePlusbool() == 1.0, "");
144 constexpr float a() {
156 static_assert(a() == 1.0, "");
158 constexpr float b() {
159 float f
= __FLT_MAX__
;
163 static_assert(b() == __FLT_MAX__
, "");
168 template<typename FloatT
>
174 constexpr A
<float> a
{12};
175 static_assert(a
.f
== 0.0f
, "");
177 constexpr A
<double> b
{12};
178 static_assert(a
.f
== 0.0, "");
181 namespace LongDouble
{
182 constexpr long double ld
= 3.1425926539;
184 constexpr long double f() {
185 const long double L
= __LDBL_MAX__
;
189 static_assert(f() == __LDBL_MAX__
, "");
192 constexpr __float128
f128() {
193 const __float128 L
= __LDBL_MAX__
;
197 static_assert(f128() == __LDBL_MAX__
, "");
202 constexpr float nan
= __builtin_nan("");
203 constexpr float inf
= __builtin_inf();
204 static_assert(!(nan
== nan
), "");
205 static_assert(nan
!= nan
, "");
206 static_assert(!(inf
< nan
), "");
207 static_assert(!(inf
> nan
), "");
211 constexpr double nan
= __builtin_nan("");
214 constexpr double D1
= 1 + nan
; // ref-error {{must be initialized by a constant expression}} \
215 // ref-note {{produces a NaN}} \
216 // expected-error {{must be initialized by a constant expression}} \
217 // expected-note {{produces a NaN}}
219 constexpr double D2
= __builtin_inf() / __builtin_inf(); // ref-error {{must be initialized by a constant expression}} \
220 // ref-note {{produces a NaN}} \
221 // expected-error {{must be initialized by a constant expression}} \
222 // expected-note {{produces a NaN}}