-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercises_17_Concepts.cpp
More file actions
597 lines (463 loc) · 15.9 KB
/
Copy pathExercises_17_Concepts.cpp
File metadata and controls
597 lines (463 loc) · 15.9 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// =====================================================================================
// Exercises_17_Concepts.cpp
// =====================================================================================
module modern_cpp_exercises:concepts;
import std;
namespace Exercises_Concepts {
namespace Exercise_01 {
class Object;
template <typename T>
concept ConceptObject = std::is_base_of<Object, T>::value;
template <typename T>
concept ConceptObjectEx = std::derived_from<T, Object>;
class Object
{
public:
virtual ~Object() = default;
virtual std::string toString() const = 0;
};
class Integer : public Object
{
protected:
int m_value;
public:
Integer() : Integer{ 0 } {};
Integer(int value) : m_value{ value } {}
std::string toString() const override
{
return "Integer: " + std::to_string(m_value);
}
};
class NaturalNumber : public Integer
{
public:
NaturalNumber() : Integer{ 0 } {};
NaturalNumber(int value) : Integer{ value >= 0 ? value : 0 } {}
};
class Double
{
private:
double m_value;
public:
Double() : Double{ 0 } {};
Double(double value) : m_value{ value } {}
std::string toString() const // override
{
return "Double: " + std::to_string(m_value);
}
};
template <typename T>
void print00(const T& obj)
{
std::println("{}", obj.toString());
}
template <typename T>
requires std::is_base_of<Object, T>::value
void print01(const T& obj)
{
std::println("{}", obj.toString());
}
template <typename T>
void print02(const T& obj) requires std::is_base_of<Object, T>::value
{
std::println("{}", obj.toString());
}
template <ConceptObject T>
void print03(const T& obj)
{
std::println("{}", obj.toString());
}
static auto print04(const ConceptObject auto& obj)
{
std::println("{}", obj.toString());
}
static void test_01() {
print00(Integer{ 1 });
print01(Integer{ 2 });
print02(Integer{ 3 });
print03(Integer{ 4 });
print04(Integer{ 5 });
print00(NaturalNumber{ 10 });
print01(NaturalNumber{ 20 });
print02(NaturalNumber{ 30 });
print03(NaturalNumber{ 40 });
print04(NaturalNumber{ 50 });
print01(NaturalNumber{ -1 });
print02(NaturalNumber{ -2 });
print03(NaturalNumber{ -3 });
print04(NaturalNumber{ -4 });
}
static void test_02() {
Double d{ 123.456 };
std::println("{}", d.toString());
// print01(d); // the constraint was not satisfied
// print02(d); // the constraint was not satisfied
// print03(d); // the constraint was not satisfied
// print04(d); // the constraint was not satisfied
}
template <ConceptObjectEx T>
void print10(const T& obj)
{
std::println("{}", obj.toString());
}
static void print11(const ConceptObjectEx auto& obj)
{
std::println("{}", obj.toString());
}
static void test_03() {
print00(NaturalNumber{ 1 });
print10(NaturalNumber{ 2 });
print11(NaturalNumber{ 3 });
print10(NaturalNumber{ -1 });
print11(NaturalNumber{ -2 });
}
static void testExercise() {
test_01();
test_02();
test_03();
}
}
namespace Exercise_02 {
namespace RequiresAllSame_01 {
// Using Function Overloading
template<typename T>
requires std::same_as<T, bool>
bool andAll(T cond) {
return cond;
}
template<typename T, typename ... TRest>
requires std::same_as<T, bool> and (std::same_as<TRest, bool> and ...)
bool andAll(T cond, TRest ... conds) {
return cond and andAll(conds...);
}
static void test() {
auto result = andAll(true, true, true);
bool b{ false };
result = andAll(!b, b, !b);
//result = andAll(1, 2, 3);
//result = andAll(123.456);
}
}
namespace RequiresAllSame_02 {
// Using Folding
template<typename ... TArgs>
requires (std::same_as<TArgs, bool> and ...)
bool andAll(TArgs ... args) {
return (... and args);
}
static void test() {
auto result = andAll(true, true, true);
bool b{ false };
result = andAll(!b, b, !b);
//result = andAll(1, 2, 3);
//result = andAll(123.456);
}
}
namespace RequiresAllSame_03 {
// Using "Abbreviated Function Templates Syntax"
static bool andAll(std::same_as<bool> auto ... args) {
return (... and args);
}
static void test() {
auto result = andAll(true, true, true);
bool b{ false };
result = andAll(!b, b, !b);
//result = andAll(1, 2, 3);
//result = andAll(123.456);
}
}
namespace RequiresAllSame_04 {
// Using "Abbreviated Function Templates Syntax"
// in combination with overloaded functions
static bool andAll(std::same_as<bool> auto cond) {
return cond;
}
static bool andAll(std::same_as<bool> auto cond, std::same_as<bool> auto ... conds) {
return cond and andAll(conds...);
}
static void test() {
auto result = andAll(true, true, true);
bool b{ false };
result = andAll(!b, b, !b);
//result = andAll(1, 2, 3);
//result = andAll(123.456);
}
}
namespace RequiresAllSame_05 {
/*
* NOT using concepts:
* Based on C++ 17 with std::enable_if and Folding
* C++ 11 is always possible, then you need to reimplement std::conjunction.
*/
// wrapper for 'std::conjunction'
template <typename ... TArgs>
constexpr bool AreSame = std::conjunction<std::is_same<bool, TArgs> ...>::value;
template <typename ... TArgs>
std::enable_if <AreSame<TArgs ...>, bool>::type
andAll(TArgs ... args) noexcept
{
return (... and args);
}
static void test() {
auto result = andAll(true, true, true);
bool b{ false };
result = andAll(!b, b, !b);
//result = andAll(1, 2, 3);
//result = andAll(123.456);
}
}
static void testExercise() {
RequiresAllSame_01::test();
RequiresAllSame_02::test();
RequiresAllSame_03::test();
RequiresAllSame_04::test();
RequiresAllSame_05::test();
}
}
namespace Exercise_03 {
namespace Exercise_03_Using_Interface {
// ---------------------------------------------------------------
// without interfaces
class IntegerIterable
{
public:
virtual bool hasNext() const = 0;
virtual int next() = 0;
virtual void reset() = 0;
};
static int count(IntegerIterable& t)
{
int count{};
t.reset();
while (t.hasNext()) {
t.next();
++count;
}
return count;
}
class IterableArray : public IntegerIterable
{
private:
std::vector<int> m_array;
std::size_t m_index;
public:
IterableArray(std::initializer_list<int> numbers)
: m_array{ numbers }, m_index{}
{}
virtual void reset() override {
m_index = 0;
}
virtual bool hasNext() const override {
return m_index < m_array.size();
}
virtual int next() override {
++m_index;
return m_array[m_index - 1];
}
};
static int getCount(IntegerIterable& a) {
return count(a);
}
static void test() {
IterableArray a{ 1, 2, 3, 4, 5 };
int count{ getCount(a) };
std::println("{}", count);
// Note: '->'-Operator
IntegerIterable* ap{ &a };
ap->reset();
while (ap->hasNext()) {
int n{ ap->next() };
std::print("{} ", n);
}
std::println();
}
}
namespace Exercise_03_Using_Concepts {
// ---------------------------------------------------------------
// without concepts
template <typename T>
int count(T& t)
{
int count{};
t.reset();
while (t.hasNext()) {
t.next();
++count;
}
return count;
}
// ---------------------------------------------------------------
// with concepts
template <typename T>
concept IsIterable = requires(T v)
{
{ std::as_const(v).hasNext() } -> std::convertible_to<bool>;
{ v.next() } -> std::same_as<int>;
{ v.reset() }-> std::convertible_to<void>;
};
template <typename T>
requires IsIterable<T>
int count(T& t) {
int count{};
t.reset();
while (t.hasNext()) {
t.next();
++count;
}
return count;
}
class IterableArray
{
private:
std::vector<int> m_array;
std::size_t m_index;
public:
IterableArray(std::initializer_list<int> numbers)
: m_array{ numbers }, m_index{}
{}
void reset() {
m_index = 0;
}
bool hasNext() const {
return m_index < m_array.size();
}
int next() {
++m_index;
return m_array[m_index - 1];
}
};
template <typename T>
requires IsIterable <T>
int getCount(T& a) {
return count(a);
}
static void test()
{
IterableArray a{ 1, 2, 3, 4, 5 };
int count1{ getCount(a) };
int count2{ getCount<IterableArray>(a) };
std::println("{}", count1);
std::println("{}", count2);
// Note: '.'-Operator
a.reset();
while (a.hasNext()) {
int n{ a.next() };
std::print("{} ", n);
}
std::println();
}
}
namespace Exercise_03_Using_Concepts_Improved {
// ---------------------------------------------------------------
// with concepts
template <typename T, typename U>
concept GenericIsIterable = requires(T v)
{
{ std::as_const(v).hasNext() } -> std::convertible_to<bool>;
{ v.next() } -> std::same_as<U>;
{ v.reset() }-> std::convertible_to<void>;
};
template <typename T, typename U>
requires GenericIsIterable<T, U>
int count(T& t) {
int count{};
t.reset();
while (t.hasNext()) {
t.next();
count++;
}
return count;
}
class IterableDoubleArray
{
private:
std::vector<double> m_array{};
std::size_t m_index{};
public:
IterableDoubleArray(std::initializer_list<double> numbers)
: m_array{ numbers }, m_index{}
{}
void reset() {
m_index = 0;
}
bool hasNext() const {
return m_index < m_array.size();
}
double next() {
m_index++;
return m_array[m_index - 1];
}
};
template <typename T, typename U>
requires GenericIsIterable <T, U>
int getCount(T& a) {
return count<T, U>(a);
}
static void test() {
IterableDoubleArray d{ 1.5, 2.5, 3.5, 4.5, 5.5 };
int count{ getCount<IterableDoubleArray, double>(d) };
std::println("{}", count);
}
}
static void testExercise() {
Exercise_03_Using_Interface::test();
Exercise_03_Using_Concepts::test();
Exercise_03_Using_Concepts_Improved::test();
}
}
namespace Exercise_04 {
template<typename ... TArgs>
requires (std::same_as<TArgs, std::string> and ...)
std::size_t totalLength(const TArgs& ... args)
{
std::size_t len{};
for (const auto& s : { args ... }) {
len += s.size();
}
return len;
}
template<typename... TArgs>
requires (std::same_as<TArgs, std::string> and ...)
std::size_t totalLengthEx(const TArgs& ... args)
{
std::size_t len{};
for (const auto& s : { args.size() ... }) {
len += s;
}
return len;
}
static void testExercise_01()
{
const std::string s1{ "111" };
const std::string s2{ "AAAAA" };
const std::string s3{ "!!!" };
std::size_t len{ totalLength(s1, s2, s3) };
std::println("{}", len);
len = totalLengthEx(s1, s2, s3);
std::println("{}", len);
}
static void testExercise_02()
{
const std::string cs{ "11111" };
const std::string& csr{ cs };
std::string s{ "333" };
std::size_t len{ totalLength(cs, std::string{ "2" }, csr, s) };
std::println("{}", len);
}
static void testExercise() {
testExercise_01();
testExercise_02();
}
}
}
void test_exercises_concepts()
{
using namespace Exercises_Concepts;
Exercise_01::testExercise();
Exercise_02::testExercise();
Exercise_03::testExercise();
Exercise_04::testExercise();
}
// =====================================================================================
// End-of-File
// =====================================================================================