-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercises_07_Metaprogramming.cpp
More file actions
245 lines (202 loc) · 8.03 KB
/
Copy pathExercises_07_Metaprogramming.cpp
File metadata and controls
245 lines (202 loc) · 8.03 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
// =====================================================================================
// Exercises_07_Metaprogramming.cpp
// =====================================================================================
module modern_cpp_exercises:metaprogramming;
import std;
namespace Exercises_Metaprogramming
{
namespace Exercise_01 {
template <std::size_t TNumber, std::size_t ... TArgs>
struct Values
{
static const std::size_t MaxNumber{
Values<TNumber>::MaxNumber > Values<TArgs...>::MaxNumber ?
Values<TNumber>::MaxNumber : Values<TArgs...>::MaxNumber
};
};
template <std::size_t TNumber>
struct Values<TNumber>
{
static const std::size_t MaxNumber{ TNumber };
};
static void testExercise()
{
constexpr std::size_t max{ Values<1, 3, 5, 4, 2>::MaxNumber };
std::cout << "Max: " << max << std::endl;
constexpr std::size_t max2{ Values<8, 6, 4, 10, 3, 5, 7>::MaxNumber };
std::cout << "Max: " << max2 << std::endl;
}
}
namespace Exercise_02 {
// runtime versions
static std::size_t convert1(std::size_t number)
{
// prepend higher bits to lowest bit
return (number == 0) ? 0 : (number % 10) | (convert1(number / 10) << 1);
}
static std::size_t convert2(std::size_t number)
{
return number == 0 ? 0 : number % 10 + 2 * convert2(number / 10);
}
// compile time version - no error handling
template <std::size_t N>
struct Binary
{
static constexpr std::size_t value = Binary<N / 10>::value << 1 | N % 10;
// or
// static constexpr std::size_t value = N % 10 + 2 * Binary<N / 10>::value;
};
// explicit template specialization
template <>
struct Binary<0>
{
static constexpr std::size_t value = 0;
};
// compile time version - with error handling
template <std::size_t N>
struct BinaryEx
{
static_assert((N % 10) == 0 or (N % 10) == 1);
static constexpr std::size_t value = BinaryEx<N / 10>::value << 1 | N % 10;
// or
// static constexpr std::size_t value = N % 10 + 2 * BinaryEx<N / 10>::value;
};
// explicit template specialization
template <>
struct BinaryEx<0>
{
static constexpr std::size_t value = 0;
};
static void testExercise_02a() {
std::size_t number{ 11111111 };
std::size_t result{ convert1(number) };
std::cout << result << std::endl;
result = convert2(number);
std::cout << result << std::endl;
number = 1001;
result = convert1(number);
std::cout << result << std::endl;
result = convert2(number);
std::cout << result << std::endl;
}
static void testExercise_02b() {
constexpr std::size_t one{ Binary<1>::value };
constexpr std::size_t three{ Binary<11>::value };
constexpr std::size_t five{ Binary<101>::value };
constexpr std::size_t seven{ Binary<111>::value };
constexpr std::size_t nine{ Binary<1001>::value };
constexpr std::size_t big{ Binary<1000'0010'0011'0101>::value };
constexpr std::size_t wrong1{ Binary<2>::value };
constexpr std::size_t wrong2{ Binary<12345>::value };
std::cout << one << std::endl;
std::cout << three << std::endl;
std::cout << five << std::endl;
std::cout << seven << std::endl;
std::cout << nine << std::endl;
std::cout << big << std::endl;
std::cout << wrong1 << std::endl;
std::cout << wrong2 << std::endl;
}
static void testExercise_02c() {
constexpr std::size_t one{ BinaryEx<1>::value };
constexpr std::size_t three{ BinaryEx<11>::value };
constexpr std::size_t five{ BinaryEx<101>::value };
constexpr std::size_t seven{ BinaryEx<111>::value };
constexpr std::size_t nine{ BinaryEx<1001>::value };
constexpr std::size_t big{ BinaryEx<1000'0010'0011'0101>::value };
//constexpr std::size_t wrong1 { BinaryEx<2>::value }; // doesn't compile
//constexpr std::size_t wrong2 { BinaryEx<12345>::value }; // doesn't compile
std::cout << one << std::endl;
std::cout << three << std::endl;
std::cout << five << std::endl;
std::cout << seven << std::endl;
std::cout << nine << std::endl;
std::cout << big << std::endl;
}
static void testExercise()
{
testExercise_02a();
testExercise_02b();
testExercise_02c();
}
}
namespace Exercise_03 {
template<std::size_t N>
struct FibImpl {
static constexpr std::size_t value =
FibImpl<N - 1>::value + FibImpl<N - 2>::value;
};
template<>
struct FibImpl<1> {
static constexpr std::size_t value = 1;
};
template<>
struct FibImpl<0> {
static constexpr std::size_t value = 0;
};
template<std::size_t N>
struct Fibonacci {
static_assert(N >= 0, "Error: Fibonacci can't be called with a negative integer");
static constexpr std::size_t value = FibImpl<N>::value;
};
static void testExercise()
{
std::cout << 1 << ": " << Fibonacci<1>::value << std::endl;
std::cout << 2 << ": " << Fibonacci<2>::value << std::endl;
std::cout << 5 << ": " << Fibonacci<5>::value << std::endl;
std::cout << 10 << ": " << Fibonacci<10>::value << std::endl;
std::cout << 15 << ": " << Fibonacci<15>::value << std::endl;
std::cout << 20 << ": " << Fibonacci<20>::value << std::endl;
std::cout << 30 << ": " << Fibonacci<30>::value << std::endl;
std::cout << 40 << ": " << Fibonacci<40>::value << std::endl;
std::cout << 50 << ": " << Fibonacci<50>::value << std::endl;
std::cout << 60 << ": " << Fibonacci<60>::value << std::endl;
std::cout << 70 << ": " << Fibonacci<70>::value << std::endl;
std::cout << 80 << ": " << Fibonacci<80>::value << std::endl;
std::cout << 90 << ": " << Fibonacci<90>::value << std::endl;
std::cout << 92 << ": " << Fibonacci<92>::value << std::endl;
}
}
namespace Exercise_04 {
// primary template
template<typename ... TArgs>
struct FirstType;
template<typename T, typename ... TArgs>
struct FirstType<T, TArgs ...>
{
using type = T;
};
// primary template
template<typename ... TArgs>
struct LastType;
template<typename T>
struct LastType<T>
{
using type = T;
};
template<typename T, typename ... TArgs>
struct LastType<T, TArgs ...>
{
using type = LastType<TArgs ...>::type;
};
static void testExercise() {
using TFirst = FirstType<double, int, long>::type;
using TLast = LastType<int, long, double, char>::type;
TFirst n{ 123.456 };
TLast ch { '!' };
std::cout << "n : " << n << std::endl;
std::cout << "ch : " << ch << std::endl;
}
}
}
void test_exercices_metaprogramming()
{
using namespace Exercises_Metaprogramming;
Exercise_01::testExercise();
Exercise_02::testExercise();
Exercise_03::testExercise();
Exercise_04::testExercise();
}
// =====================================================================================
// End-of-File
// =====================================================================================