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}}
42 /// Initialized by a double.
43 constexpr float df
= 0.0;
44 /// The other way around.
45 constexpr double fd
= 0.0f
;
47 static_assert(0.0f
== -0.0f
, "");
49 const int k
= 3 * (1.0f
/ 3.0f
);
50 static_assert(k
== 1, "");
52 constexpr bool b
= 1.0;
55 constexpr double db
= true;
56 static_assert(db
== 1.0, "");
58 constexpr float fa
[] = {1.0f
, 2.0, 1, false};
59 constexpr double da
[] = {1.0f
, 2.0, 1, false};
61 constexpr float fm
= __FLT_MAX__
;
62 constexpr int someInt
= fm
; // ref-error {{must be initialized by a constant expression}} \
63 // ref-note {{is outside the range of representable values}} \
64 // expected-error {{must be initialized by a constant expression}} \
65 // expected-note {{is outside the range of representable values}}
68 constexpr float f1() {
81 static_assert(f1() == 2, "");
83 constexpr float f2() {
84 float f
= __FLT_MAX__
;
88 static_assert(f2() == __FLT_MAX__
, "");
90 constexpr float ff() {
94 // RHS should be evaluated before LHS, so this should
97 #if __cplusplus <= 201402L
98 // expected-warning@-2 {{multiple unsequenced modifications}} \
99 // ref-warning@-2 {{multiple unsequenced modifications}}
104 static_assert(ff() == 3, "");
106 constexpr float intPlusDouble() {
112 static_assert(intPlusDouble() == 2, "");
114 constexpr double doublePlusInt() {
120 static_assert(doublePlusInt() == 2, "");
122 constexpr float boolPlusDouble() {
128 static_assert(boolPlusDouble(), "");
130 constexpr bool doublePlusbool() {
136 static_assert(doublePlusbool() == 1.0, "");
140 constexpr float a() {
152 static_assert(a() == 1.0, "");
154 constexpr float b() {
155 float f
= __FLT_MAX__
;
159 static_assert(b() == __FLT_MAX__
, "");
164 template<typename FloatT
>
170 constexpr A
<float> a
{12};
171 static_assert(a
.f
== 0.0f
, "");
173 constexpr A
<double> b
{12};
174 static_assert(a
.f
== 0.0, "");
177 namespace LongDouble
{
178 constexpr long double ld
= 3.1425926539;
180 constexpr long double f() {
181 const long double L
= __LDBL_MAX__
;
185 static_assert(f() == __LDBL_MAX__
, "");
188 constexpr __float128
f128() {
189 const __float128 L
= __LDBL_MAX__
;
193 static_assert(f128() == __LDBL_MAX__
, "");
198 constexpr float nan
= __builtin_nan("");
199 constexpr float inf
= __builtin_inf();
200 static_assert(!(nan
== nan
), "");
201 static_assert(nan
!= nan
, "");
202 static_assert(!(inf
< nan
), "");
203 static_assert(!(inf
> nan
), "");
207 constexpr double nan
= __builtin_nan("");
210 constexpr double D1
= 1 + nan
; // ref-error {{must be initialized by a constant expression}} \
211 // ref-note {{produces a NaN}} \
212 // expected-error {{must be initialized by a constant expression}} \
213 // expected-note {{produces a NaN}}
215 constexpr double D2
= __builtin_inf() / __builtin_inf(); // ref-error {{must be initialized by a constant expression}} \
216 // ref-note {{produces a NaN}} \
217 // expected-error {{must be initialized by a constant expression}} \
218 // expected-note {{produces a NaN}}