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
+61-17Lines changed: 61 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,21 +16,41 @@ This time, we'll be going through C++ templates!!
16
16
17
17
18
18
19
-
## Table Of Contents
19
+
## Table Of Contents <aname="top"></a>
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)
21
37
22
38
23
-
## Introduction
39
+
40
+
41
+
## 1. Introduction <aname="1"></a>
24
42
25
43
So we spent the previous section talking about a couple of uses of the STL library. But have you ever wondered what templates are?
26
44
27
45
> **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.
28
46
29
47
30
48
31
-
## C++ Templates Reference
49
+
## 2. C++ Templates Reference <aname="2"></a>
50
+
51
+
### 2.1 Template Concept <aname="2.1"></a>
52
+
[go to top](#top)
32
53
33
-
### Template Concept
34
54
35
55
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.
36
56
@@ -42,7 +62,9 @@ The idea of templates is a very powerful one. It allows you to wrap C++ with a t
42
62
43
63
44
64
45
-
### Function Templates
65
+
### 2.2 Function Templates <aname="2.2"></a>
66
+
[go to top](#top)
67
+
46
68
47
69
Let's look at a simple example for defining function templates!
48
70
@@ -72,7 +94,9 @@ T maximise(T x, T y)
72
94
73
95
74
96
75
-
### Class Templates
97
+
### 2.3 Class Templates <a name="2.3"></a>
98
+
[go to top](#top)
99
+
76
100
77
101
You can template into classes too!
78
102
@@ -122,7 +146,9 @@ int main() {
122
146
123
147
124
148
125
-
### Template Variables
149
+
### 2.4 Template Variables <aname="2.4"></a>
150
+
[go to top](#top)
151
+
126
152
127
153
You can even declare variables as templates!
128
154
@@ -138,7 +164,9 @@ pi<long>; // So on and so forth
138
164
139
165
140
166
141
-
### Multiple Arguments
167
+
### 2.5 Multiple Arguments <aname="2.5"></a>
168
+
[go to top](#top)
169
+
142
170
143
171
You can pass multiple arguments to templates! Just add on more stuff.
144
172
@@ -153,7 +181,9 @@ T maximise(T x, U y)
153
181
154
182
155
183
156
-
### Default Arguments
184
+
### 2.6 Default Arguments <a name="2.6"></a>
185
+
[go to top](#top)
186
+
157
187
158
188
```c++
159
189
template <typename T, typename U = int>
@@ -166,7 +196,9 @@ T maximise(T x, U y)
166
196
167
197
168
198
169
-
### Some Notes
199
+
### 2.7 Some Notes <aname="2.7"></a>
200
+
[go to top](#top)
201
+
170
202
171
203
> **What is the difference between function overloading and templates?**
172
204
> 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.
@@ -182,7 +214,7 @@ So the reason you have multiple static members is because the templates actually
182
214
183
215
184
216
185
-
## Template Metaprogramming
217
+
## 3. Template Metaprogramming <aname="3"></a>
186
218
187
219

188
220
@@ -194,7 +226,9 @@ But the main benefit of using templates to such a degree is greater generality o
194
226
195
227
196
228
197
-
### Template Parameters vs Template Arguments
229
+
### 3.1 Template Parameters vs Template Arguments <aname="3.1"></a>
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!**
393
435
@@ -412,7 +454,9 @@ If there is generally an exact match for the template arguments after passing th
412
454
413
455
414
456
415
-
### SFINAE (Substitution Failure Is Not An Error)
457
+
### 3.6 SFINAE (Substitution Failure Is Not An Error) <a name="3.6"></a>
0 commit comments