-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConstExpr.cpp
More file actions
305 lines (228 loc) · 8.09 KB
/
ConstExpr.cpp
File metadata and controls
305 lines (228 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// =====================================================================================
// ConstExpr.cpp
// =====================================================================================
module;
#include <cassert>
module modern_cpp:const_expr;
// =====================================================================================
// variables
namespace ConstExprVariables {
constexpr double Pi = 3.14159265359;
#define PI 3.14159265359
static void testVariables() {
std::println("Pi: {}", Pi);
std::println("PI: {}", PI);
}
}
// =====================================================================================
// functions
namespace ConstExprFunctions {
// ======================================================
// limit a number in a range or in between two given numbers
#define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : ((x) > (hi) ? (hi) : (x)))
static auto clamp = [](auto x, auto lo, auto hi) constexpr {
return x < lo ? lo : (x > hi ? hi : x);
};
static void testFunctions_01() {
constexpr std::size_t a{ 3 };
constexpr std::size_t b{ 8 };
constexpr auto value1{ CLAMP(a + b, 0, 10) };
constexpr auto value2{ clamp(a + b, 0, 10) };
constexpr auto value3{ clamp(a + b, 5, 8) };
}
// ======================================================
// calculate the number multiplied by itself
#define SQUARE(x) x * x
static constexpr auto square (auto x) {
return x * x;
};
static auto squareLambda = [](auto x) constexpr {
return x * x;
};
static void testFunctions_02() {
// Note: macros can produce unexpected results
std::size_t a{ 1 };
std::size_t b{ 2 };
std::size_t value{ SQUARE(a + b) }; // value == 5: Wrong result !!!
assert(value == 5);
}
static void testFunctions_03() {
// Note: constexpr functions don't have side effects !!!
constexpr std::size_t a{ 1 };
constexpr std::size_t b{ 2 };
constexpr auto value{ square(a + b) };
static_assert(value == 9); // correct result !!!
constexpr double x{ 3.0 };
constexpr double y{ 4.0 };
constexpr auto dvalue{ squareLambda(x + y) };
static_assert(dvalue == 49.0); // correct result !!!
}
static constexpr auto squareOfTwo = square(2);
static constexpr auto squareOfThree = square(3.0);
static constexpr auto squareOfFour = squareLambda(4);
static constexpr auto squareOfFive = squareLambda(5.0);
// ======================================================
// swap the values of two variables
#define SWAP(a, b) \
{ \
auto tmp = (a); \
(a) = (b); \
(b) = tmp; \
}
static auto swap = [](auto& a, auto& b) constexpr {
auto tmp = a;
a = b;
b = tmp;
};
// constexpr lambda as a compile-time function template (C++ 20)
// enforces that both parameters have the same type
//static auto swap = []<typename T>(T& a, T& b) constexpr {
// auto tmp = a;
// a = b;
// b = tmp;
// };
static void testFunctions_04() {
std::size_t a{ 1 };
std::size_t b{ 2 };
std::println("{} - {}", a, b);
SWAP(a, b);
std::println("{} - {}", a, b);
}
static void testFunctions_05() {
std::size_t a{ 1 };
std::size_t b{ 2 };
std::println("{} - {}", a, b);
swap(a, b);
std::println("{} - {}", a, b);
}
// ======================================================
static void testFunctions() {
testFunctions_01();
testFunctions_02();
testFunctions_03();
testFunctions_04();
testFunctions_05();
}
}
// =====================================================================================
// classes and objects
namespace ConstExprClassesAndObjects {
class Complex
{
private:
float m_real;
float m_imag;
public:
// c'tors
constexpr Complex() : m_real{ }, m_imag{ } {}
constexpr Complex(float real, float imag) : m_real{ real }, m_imag{ imag } {}
// getter
constexpr float real() const { return m_real; }
constexpr float imag() const { return m_imag; }
// operators
constexpr Complex operator+ (const Complex& other) const
{
float real = m_real + other.m_real;
float imag = m_imag + other.m_imag;
return { real, imag };
}
};
static void testComplex()
{
constexpr Complex c0{ };
constexpr Complex c1{ 1.0, 2.0 };
constexpr Complex c2{ 3.0, 3.0 };
constexpr float r1{ c1.real() };
constexpr Complex c3{ c1 + c2 };
constexpr float r2{ c3.real() };
// verify 'constness' with the help of disassembly and
// https://www.h-schmidt.net/FloatConverter/IEEE754de.html
std::println("Real: ", c3.real());
std::println("Imag: ", c3.imag());
// verify compile time computing
static_assert (c1.real() == 1.0, "real part shoud be 1.0");
static_assert (c3.real() == 4.0, "real part shoud be 4.0");
static_assert (c3.imag() == 5.0, "imaginary part shoud be 5.0");
}
}
// =====================================================================================
// dynamic variables
namespace ConstExprDynamicData {
static constexpr int naiveSum(unsigned int n)
{
auto ip{ new int[n] };
std::iota(ip, ip + n, 1);
auto tmp{ std::accumulate(ip, ip + n, 0) };
delete[] ip;
return tmp;
}
static void testDynamicData()
{
constexpr int sum{ naiveSum(10) };
std::println("Sum from 1 up to 10: {}", sum);
}
}
// =====================================================================================
// example: compile-time table of power numbers
namespace ConstExprPow {
constexpr std::size_t TableSize = 5;
constexpr std::size_t Factor = 4;
template<std::size_t F>
constexpr auto powerTable = [] {
std::array<std::size_t, TableSize> table = {};
std::size_t index{ 1 };
for (std::size_t& elem : table) {
std::size_t tmp{ 1 };
for (std::size_t i{}; i != F; ++i) {
tmp *= index;
}
elem = tmp;
++index;
}
return table;
} ();
static constexpr std::size_t sumUpPowerTable()
{
static_assert (powerTable<Factor>[0] == 1, "Value should be ");
static_assert (powerTable<Factor>[1] == 16, "Value should be ");
static_assert (powerTable<Factor>[2] == 81, "Value should be ");
static_assert (powerTable<Factor>[3] == 256, "Value should be ");
static_assert (powerTable<Factor>[4] == 625, "Value should be ");
std::size_t total{};
for (std::size_t i{}; i != TableSize; ++i) {
total += powerTable<Factor>[i];
}
return total;
}
static void testPower_01()
{
constexpr std::size_t total{ sumUpPowerTable() };
static_assert (total == 979, "Sum should be 979");
std::println("Total: ", total);
}
static void testPower_02()
{
int index = 1;
for (std::size_t elem : powerTable<Factor>) {
std::println("{:02}: {}", index++, elem);
}
constexpr std::size_t total = sumUpPowerTable();
std::println("Total: {}", total);
}
static void testPower()
{
testPower_01();
testPower_02();
}
}
void main_constexpr()
{
ConstExprVariables::testVariables();
ConstExprFunctions::testFunctions();
ConstExprClassesAndObjects::testComplex();
ConstExprDynamicData::testDynamicData();
ConstExprPow::testPower();
}
// =====================================================================================
// End-of-File
// =====================================================================================