You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: C++/06 C++ - Templates.md
+29-60Lines changed: 29 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,41 +16,21 @@ This time, we'll be going through C++ templates!!
16
16
17
17
18
18
19
-
## Table Of Contents <aname="top"></a>
19
+
## Table Of Contents
20
20
21
-
1.[Introduction](#1)
22
-
2.[C++ Templates Reference](#2)
23
-
2.1 [Template Concept](#2.1)
24
-
2.2 [Function Templates](#2.2)
25
-
2.3 [Class Templates](#2.3)
26
-
2.4 [Template Variables](#2.4)
27
-
2.5 [Multiple Arguments](#2.5)
28
-
2.6 [Default Arguments](#2.6)
29
-
2.7 [Some Notes](#2.7)
30
-
3.[Template Metaprogramming](#3)
31
-
3.1 [Template Parameters vs Template Arguments](#3.1)
32
-
3.2 [Template Specialisation](#3.2)
33
-
3.3 [Partial Specialisation](#3.3)
34
-
3.4 [Non-Type Template Arguments](#3.4)
35
-
3.5 [Template Argument Deduction](#3.5)
36
-
3.6 [SFINAE (Substitution Failure Is Not An Error)](#3.6)
37
21
38
22
39
-
40
-
41
-
## 1. Introduction <aname="1"></a>
23
+
## Introduction
42
24
43
25
So we spent the previous section talking about a couple of uses of the STL library. But have you ever wondered what templates are?
44
26
45
27
> **Templates** are powerful features of **C++** which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using **templates**. **Templates** are often used in larger codebase for the purpose of code reusability and flexibility of the programs.
46
28
47
29
48
30
49
-
## 2. C++ Templates Reference <aname="2"></a>
50
-
51
-
### 2.1 Template Concept <aname="2.1"></a>
52
-
[go to top](#top)
31
+
## C++ Templates Reference
53
32
33
+
### Template Concept
54
34
55
35
The idea of templates is a very powerful one. It allows you to wrap C++ with a type of 'metalanguage' that allows you to write generic functions that can deal with a multitude of data types.
56
36
@@ -62,9 +42,7 @@ The idea of templates is a very powerful one. It allows you to wrap C++ with a t
62
42
63
43
64
44
65
-
### 2.2 Function Templates <aname="2.2"></a>
66
-
[go to top](#top)
67
-
45
+
### Function Templates
68
46
69
47
Let's look at a simple example for defining function templates!
70
48
@@ -94,9 +72,7 @@ T maximise(T x, T y)
94
72
95
73
96
74
97
-
### 2.3 Class Templates <a name="2.3"></a>
98
-
[go to top](#top)
99
-
75
+
### Class Templates
100
76
101
77
You can template into classes too!
102
78
@@ -146,9 +122,7 @@ int main() {
146
122
147
123
148
124
149
-
### 2.4 Template Variables <aname="2.4"></a>
150
-
[go to top](#top)
151
-
125
+
### Template Variables
152
126
153
127
You can even declare variables as templates!
154
128
@@ -164,9 +138,7 @@ pi<long>; // So on and so forth
164
138
165
139
166
140
167
-
### 2.5 Multiple Arguments <aname="2.5"></a>
168
-
[go to top](#top)
169
-
141
+
### Multiple Arguments
170
142
171
143
You can pass multiple arguments to templates! Just add on more stuff.
172
144
@@ -181,9 +153,7 @@ T maximise(T x, U y)
181
153
182
154
183
155
184
-
### 2.6 Default Arguments <a name="2.6"></a>
185
-
[go to top](#top)
186
-
156
+
### Default Arguments
187
157
188
158
```c++
189
159
template <typename T, typename U = int>
@@ -196,9 +166,7 @@ T maximise(T x, U y)
196
166
197
167
198
168
199
-
### 2.7 Some Notes <aname="2.7"></a>
200
-
[go to top](#top)
201
-
169
+
### Some Notes
202
170
203
171
> **What is the difference between function overloading and templates?**
204
172
> Both function overloading and templates are examples of polymorphism feature of OOP. Function overloading is used when multiple functions do similar operations, templates are used when multiple functions do identical operations.
@@ -214,7 +182,7 @@ So the reason you have multiple static members is because the templates actually
214
182
215
183
216
184
217
-
## 3. Template Metaprogramming <aname="3"></a>
185
+
## Template Metaprogramming
218
186
219
187

220
188
@@ -226,9 +194,7 @@ But the main benefit of using templates to such a degree is greater generality o
226
194
227
195
228
196
229
-
### 3.1 Template Parameters vs Template Arguments <aname="3.1"></a>
@@ -241,12 +207,23 @@ void fun<int>(int a) // This second <> is known as the template argument list
241
207
}
242
208
```
243
209
210
+
Template parameters are used to first decide which template to even use.
244
211
212
+
Then the arguments serve as a way to further declare explicitly the expected types of the arguments. (Which could use the template argument types.)
213
+
214
+
```c++
215
+
// Example
216
+
template <typename T>
217
+
T maximise<T>(T x, T y)
218
+
{
219
+
return (x > y) ? x : y; // Return the maximum
220
+
}
221
+
```
245
222
246
-
### 3.2 Template Specialisation <aname="3.2"></a>
247
-
[go to top](#top)
248
223
249
224
225
+
### Template Specialisation
226
+
250
227
Suppose you have a generic function, but you realise that one particular possible input type can be treated in a far, far more efficient way.
251
228
252
229
**Turns out you can write a special template function just for that type, so every other input type other than that type resolves the general template definition, but you create a specialised template just for that one input type!**
@@ -342,9 +319,7 @@ public:
342
319
343
320
344
321
345
-
### 3.3 Partial Specialisation <a name="3.3"></a>
346
-
[go to top](#top)
347
-
322
+
### Partial Specialisation
348
323
349
324
Partial specialisation works for structs and classes.
This diagram shows how the final template is resolved if there are specialisations or multiple declarations of templates involved. **Note again that only base templates are overloaded for function templates!**
435
406
@@ -454,9 +425,7 @@ If there is generally an exact match for the template arguments after passing th
454
425
455
426
456
427
457
-
### 3.6 SFINAE (Substitution Failure Is Not An Error) <a name="3.6"></a>
0 commit comments