-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTemplatesFunctionBasics.cpp
More file actions
202 lines (157 loc) · 5.58 KB
/
TemplatesFunctionBasics.cpp
File metadata and controls
202 lines (157 loc) · 5.58 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
// =====================================================================================
// TemplatesFunctionBasics.cpp // Function Templates
// =====================================================================================
module modern_cpp:templates_function_basics;
namespace FunctionTemplate_01_Basics {
template <typename T>
struct Point
{
T m_x;
T m_y;
Point operator+ (const Point& p) const {
return { m_x + p.m_x, m_y + p.m_y };
}
T distance() const {
return std::sqrt(m_x * m_x + m_y * m_y);
}
};
// normal function definition
bool lessThan(int a, int b)
{
return (a < b) ? true : false;
}
// function template definition
template <typename T>
bool lessThan(const T& a, const T& b)
{
return (a < b) ? true : false;
}
// explicit function template specialization
template <>
bool lessThan<Point<double>>(const Point<double>& a, const Point<double>& b)
{
return (a.distance() < b.distance()) ? true : false;
}
// another explicit function template specialization
template <>
bool lessThan<std::complex<double>>(const std::complex<double>& a, const std::complex<double>& b)
{
return (std::abs(a) < std::abs(b)) ? true : false;
}
// note: explicit versus partial function template specialization
template <typename T, typename U>
void function(T a, U b) {}
// partial function template specialization: not allowed (!)
// template <typename T>
// void function<T, int>(T a, int b) {}
// explicit function template specialization: allowed (!)
template <>
void function<double, int>(double, int) {}
static void test_01()
{
bool result;
result = lessThan(10, 20);
// result = lessThan(10, 20.5); // warning or error
result = lessThan(10.0, 20.0);
result = lessThan(Point<double>{ 1, 2 }, Point<double>{ 3, 4 });
using namespace std::complex_literals;
std::complex<double> z1 = 1. + 2i;
std::complex<double> z2 = 1. - 2i;
result = lessThan(z1, z2);
}
}
namespace FunctionTemplate_02_Overloading {
template <typename T>
inline const T& minimum(const T& a, const T& b) {
return (a < b) ? a : b;
}
template <typename T>
inline const T& minimum(const T& a, const T& b, const T& c) {
return a < b ? (a < c ? a : c) : (b < c ? b : c);
}
inline const int& minimum(const int& a, const int& b) {
return (a < b) ? a : b;
}
static void test_02()
{
minimum(1, 2, 3); // Template mit 3 Argumenten
minimum(1.0, 2.0); // minimum<double> per Typ Deduktion
minimum('X', 'Y'); // minimum<char> per Typ Deduktion
minimum(1, 2); // non-template Variante bevorzugt
minimum<>(1, 2); // minimum<int> per Typ Deduktion
minimum<double>(1, 2); // minimum<double> ohne Typ Deduktion
// <minimum('X', 3.14); // non-template Variante für 2 ints (compiler warning)
}
}
namespace FunctionTemplates_03_Specialisation {
template <typename T>
void printValue(T const& t)
{
std::cout << "Value: " << t << std::endl;
}
// put this template into comments if Typ Deduktion is going to be demonstrated
template <>
void printValue<bool>(bool const& b)
{
std::cout << std::boolalpha << "Value: " << b << std::endl;
}
static void test_03_01() {
printValue<int>(10);
printValue<bool>(true);
printValue<double>(12.5);
}
static void test_03_02() {
printValue<>(10);
printValue<>(true);
printValue<>(12.5);
}
static void test_03_03() {
printValue(10);
printValue(true);
printValue(12.5);
}
}
namespace FunctionTemplates_04_Specialization_vs_FunctionOverloading {
// from https://stackoverflow.com/questions/7108033/template-specialization-vs-function-overloading
// but doesn't still demonstrate, what it should demonstrate :(
namespace Variant_01 {
template <typename T> void foo(T) {};
template <typename T> void foo(T*) { std::cout << "overload"; }; // overload of foo(T)
template <> void foo<int*>(int*) { std::cout << ""; }; // specialization of foo(T*)
static void test_04() {
int* ip = new int{ 123 };
foo(new int); // calls overload
delete ip;
}
}
namespace Variant_02 {
template <typename T> void foo(T) {};
template <> void foo<int*>(int*) { std::cout << ""; }; // specialization of foo(T*)
template <typename T> void foo(T*) { std::cout << "overload"; }; // overload of foo(T)
static void test_04() {
int* ip = new int{ 123 };
foo(new int); // calls specialization
delete ip;
}
}
static void test_04() {
Variant_01::test_04();
Variant_02::test_04();
}
}
void main_templates_function_basics()
{
using namespace FunctionTemplate_01_Basics;
test_01();
using namespace FunctionTemplate_02_Overloading;
test_02();
using namespace FunctionTemplates_03_Specialisation;
test_03_01();
test_03_02();
test_03_03();
using namespace FunctionTemplates_04_Specialization_vs_FunctionOverloading;
test_04();
}
// =====================================================================================
// End-of-File
// =====================================================================================