-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTypeTraits.cpp
More file actions
507 lines (420 loc) · 12.7 KB
/
TypeTraits.cpp
File metadata and controls
507 lines (420 loc) · 12.7 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
// =====================================================================================
// TypeTraits.cpp // Typmerkmale // Type Traits
// =====================================================================================
module modern_cpp:type_traits;
namespace TypeTraits_Simple_Demo
{
// primary template
template <typename T>
struct is_this_a_floating_point
{
static constexpr bool value = false;
};
// explicit (full) specialization
template <>
struct is_this_a_floating_point<float>
{
static constexpr bool value = true;
};
template <>
struct is_this_a_floating_point<double>
{
static constexpr bool value = true;
};
template <>
struct is_this_a_floating_point<long double>
{
static constexpr bool value = true;
};
static void test_1a()
{
static_assert(is_this_a_floating_point<float>::value);
static_assert(is_this_a_floating_point<double>::value);
static_assert(is_this_a_floating_point<long double>::value);
static_assert(! is_this_a_floating_point<int>::value);
static_assert(! is_this_a_floating_point<bool>::value);
}
template <typename T>
void process_a_floating_point(T value)
{
static_assert(is_this_a_floating_point<T>::value);
std::cout << "processing a real number: " << value << std::endl;
}
static void test_1b()
{
process_a_floating_point(42.0);
// process_a_floating_point(42); // does'n t compile: static assertion fails
}
static void test_01()
{
test_1a();
test_1b();
}
}
// =================================================================================
namespace TypeTraits_Second_Simple_Demo
{
// primary template
template <typename T>
struct my_remove_reference {
using type = T;
};
// explicit (partial) specialization
template <typename T>
struct my_remove_reference<T&> {
using type = T;
};
// primary template
template<class T, class U>
struct my_is_same
{
static constexpr bool value = false;
};
// template (partial) specialization
template<class T>
struct my_is_same<T, T>
{
static constexpr bool value = true;
};
static void test_02_a()
{
using SomeType = const double&;
using SomeTypeWithoutRef = std::remove_reference<SomeType>::type;
using SomeTypeWithoutRefAndConst = std::remove_const<SomeTypeWithoutRef>::type;
static_assert(std::is_same<SomeTypeWithoutRefAndConst, double>::value == true);
using SomeTypeWithoutRef2 = my_remove_reference<SomeType>::type;
using SomeTypeWithoutRefAndConst2 = std::remove_const<SomeTypeWithoutRef2>::type;
static_assert(std::is_same<SomeTypeWithoutRefAndConst2, double>::value == true);
}
static void test_02_b()
{
static_assert(my_is_same<int, double>::value == false);
static_assert(my_is_same<int, int>::value == true);
}
static void test_02()
{
test_02_a();
test_02_b();
}
}
// =================================================================================
namespace TypeTraits_Conditional_Compilation_Demo {
class Widget
{
private:
int m_id;
std::string m_name;
public:
Widget() : Widget { 0, ""} {}
Widget (int id, std::string name) : m_id{ id}, m_name{ name } {}
std::ostream& write(std::ostream& os) const
{
os << m_id << ", " << m_name << '\n';
return os;
}
};
class Gadget
{
private:
int m_id;
std::string m_name;
public:
Gadget() : Gadget{ 0, "" } {}
Gadget(int id, const std::string& name) : m_id{ id }, m_name{ name } {}
int getId() const { return m_id; }
const std::string& getName() const { return m_name; }
};
std::ostream& operator <<(std::ostream& os, const Gadget& gadget)
{
os << gadget.getId() << ", " << gadget.getName() << '\n';
return os;
}
static void test_3a()
{
Widget widget{ 1, "I'm a Widget" };
Gadget gadget{ 2, "I'm a Gadget" };
widget.write(std::cout);
std::cout << gadget;
}
template <typename T>
struct uses_write
{
static constexpr bool value = false;
};
template <>
struct uses_write<Widget>
{
static constexpr bool value = true;
};
// primary (class) template
template <bool>
struct Serializer
{
template <typename T>
static void serialize(std::ostream& os, T const& value)
{
os << value;
}
};
template<>
struct Serializer<true>
{
template <typename T>
static void serialize(std::ostream& os, T const& value)
{
value.write(os);
}
};
// free function template - based on class Serializer<T>
template <typename T>
void serialize(std::ostream& os, T const& obj)
{
Serializer<uses_write<T>::value>::serialize(os, obj);
}
static void test_3b()
{
Widget widget{ 1, "I'm a Widget" };
Gadget gadget{ 2, "I'm a Gadget" };
serialize(std::cout, widget);
serialize(std::cout, gadget);
}
static void test_3c()
{
Widget widget{ 1, "I'm a Widget" };
Gadget gadget{ 2, "I'm a Gadget" };
serialize<Widget>(std::cout, widget);
serialize<Gadget>(std::cout, gadget);
}
static void test_03()
{
test_3a();
test_3b();
test_3c();
}
}
// =================================================================================
namespace TypeTraits_Iterator_Demo
{
void whichIterator(const std::input_iterator_tag)
{
std::cout << "[Input Iterator]" << std::endl;
}
void whichIterator(const std::output_iterator_tag)
{
std::cout << "[Output Iterator]" << std::endl;
}
void whichIterator(const std::forward_iterator_tag)
{
std::cout << "[Forward Iterator]" << std::endl;
}
void whichIterator(const std::bidirectional_iterator_tag)
{
std::cout << "[Bidirectional Iterator Iterator]" << std::endl;
}
void whichIterator(const std::random_access_iterator_tag)
{
std::cout << "[Random Access Iterator]" << std::endl;
}
template <typename TIterator>
auto getIteratorType(const TIterator&)
{
typename std::iterator_traits<TIterator>::iterator_category tag{};
return tag;
}
static void test_4a()
{
std::vector<int> vec;
whichIterator(getIteratorType(vec.begin()));
// oder:
std::vector<int>::iterator it;
whichIterator(getIteratorType(it));
// oder:
std::vector<int>::iterator::iterator_category iter_cat{};
whichIterator(iter_cat);
}
static void test_4b()
{
std::list<int> list;
whichIterator(getIteratorType(list.begin()));
// oder:
std::list<int>::iterator it;
whichIterator(getIteratorType(it));
// oder:
std::list<int>::iterator::iterator_category iter_cat{};
whichIterator(iter_cat);
}
static void test_4c()
{
std::forward_list<int> fwlist;
whichIterator(getIteratorType(fwlist.begin()));
// oder:
std::forward_list<int>::iterator it;
whichIterator(getIteratorType(it));
// oder:
std::forward_list<int>::iterator::iterator_category iter_cat{};
whichIterator(iter_cat);
}
static void test_4d()
{
std::ifstream source;
std::istream_iterator<int> input(source);
whichIterator(getIteratorType(input));
// oder:
std::istream_iterator<int> it{};
whichIterator(getIteratorType(it));
// oder:
std::istream_iterator<int>::iterator_category iter_cat{};
whichIterator(iter_cat);
}
static void test_4e()
{
std::ofstream source;
std::ostream_iterator<int> output(source);
whichIterator(getIteratorType(output));
// oder:
// std::ostream_iterator<int> has no default c'tor
// oder:
std::ostream_iterator<int>::iterator_category iter_cat{};
whichIterator(iter_cat);
}
static void test_04()
{
test_4a();
test_4b();
test_4c();
test_4d();
test_4e();
}
}
// =================================================================================
namespace TypeTraits_Second_Iterator_Demo
{
template <typename Iterator>
using ValueType = typename Iterator::value_type;
template <typename Iterator>
using Category = typename Iterator::iterator_category;
template <typename Iterator>
ValueType<Iterator> getAt(Iterator it, std::size_t position, std::forward_iterator_tag)
{
std::cout << "[Forward Iterator] ";
Iterator pos = it;
for (std::size_t i{}; i != position; ++i)
++pos;
return *pos;
}
template <typename Iterator>
ValueType<Iterator> getAt(Iterator it, std::size_t position, std::bidirectional_iterator_tag)
{
std::cout << "[Bidirectional Access] ";
Iterator pos = it;
for (std::size_t i{}; i != position; ++i)
++pos;
return *pos;
}
template <typename Iterator>
ValueType<Iterator> getAt(Iterator it, std::size_t position, std::random_access_iterator_tag)
{
std::cout << "[Random Access] ";
Iterator pos = it + position;
return *pos;
}
template <typename Iterator>
ValueType<Iterator> getAt(Iterator it, std::size_t size)
{
Category<Iterator> category{};
return getAt(it, size, category);
}
static void test_05()
{
std::forward_list<char> charList{ 'A', 'B', 'C', 'D', 'E' };
char ch = getAt(charList.begin(), 3);
std::cout << "std::forward_list: " << ch << std::endl;
std::list<int> intList{ 1, 2, 3, 4, 5 };
int value = getAt(intList.begin(), 3);
std::cout << "std::list: " << value << std::endl;
std::vector<int> doubleVector{ 10, 20, 30, 40, 50 };
double d = getAt(doubleVector.begin(), 3);
std::cout << "std::vector: " << d << std::endl;
}
}
// =================================================================================
namespace TypeTraits_Demo_Remove_Reference
{
template<typename Container>
void sort_01(Container& container)
{
std::sort(
container.begin(),
container.end(),
[](auto n1, auto n2) -> bool {
return n1 < n2;
}
);
}
template<typename Container>
void sort_02(Container& container)
{
std::sort(
container.begin(),
container.end(),
[](typename Container::value_type n1, typename Container::value_type n2) {
return n1 < n2;
}
);
}
template<typename Container>
void sort_03(Container& container)
{
std::sort(
container.begin(),
container.end(),
[]( decltype (container[0]) n1,
decltype (container[0]) n2) {
return n1 < n2;
}
);
}
template<typename Container>
void sort_04(Container& container)
{
std::sort(
container.begin(),
container.end(),
[]( std::remove_reference<decltype (container[0])>::type n1,
std::remove_reference<decltype (container[0])>::type n2) {
return n1 < n2;
}
);
}
static void test_06()
{
std::vector<int> vec{ 5, 4, 6, 3, 7, 2, 8, 1, 9 };
sort_01<std::vector<int>>(vec);
sort_02<std::vector<int>>(vec);
sort_03<std::vector<int>>(vec);
sort_04<std::vector<int>>(vec);
for (int n : vec) {
std::cout << n << ' ';
}
std::cout << std::endl;
}
}
// =================================================================================
void main_type_traits()
{
using namespace TypeTraits_Simple_Demo;
using namespace TypeTraits_Second_Simple_Demo;
using namespace TypeTraits_Conditional_Compilation_Demo;
using namespace TypeTraits_Iterator_Demo;
using namespace TypeTraits_Second_Iterator_Demo;
using namespace TypeTraits_Demo_Remove_Reference;
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
}
// =====================================================================================
// End-of-File
// =====================================================================================