Skip to content

Commit 9beb42f

Browse files
committed
Add TOC for C++ templates section
1 parent 9bdbfb9 commit 9beb42f

File tree

1 file changed

+61
-17
lines changed

1 file changed

+61
-17
lines changed

C++/06 C++ - Templates.md

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,41 @@ This time, we'll be going through C++ templates!!
1616

1717

1818

19-
## Table Of Contents
19+
## Table Of Contents <a name="top"></a>
2020

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)
2137

2238

23-
## Introduction
39+
40+
41+
## 1. Introduction <a name="1"></a>
2442

2543
So we spent the previous section talking about a couple of uses of the STL library. But have you ever wondered what templates are?
2644

2745
> **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.
2846
2947

3048

31-
## C++ Templates Reference
49+
## 2. C++ Templates Reference <a name="2"></a>
50+
51+
### 2.1 Template Concept <a name="2.1"></a>
52+
[go to top](#top)
3253

33-
### Template Concept
3454

3555
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.
3656

@@ -42,7 +62,9 @@ The idea of templates is a very powerful one. It allows you to wrap C++ with a t
4262

4363

4464

45-
### Function Templates
65+
### 2.2 Function Templates <a name="2.2"></a>
66+
[go to top](#top)
67+
4668

4769
Let's look at a simple example for defining function templates!
4870

@@ -72,7 +94,9 @@ T maximise(T x, T y)
7294
7395
7496
75-
### Class Templates
97+
### 2.3 Class Templates <a name="2.3"></a>
98+
[go to top](#top)
99+
76100
77101
You can template into classes too!
78102
@@ -122,7 +146,9 @@ int main() {
122146

123147

124148

125-
### Template Variables
149+
### 2.4 Template Variables <a name="2.4"></a>
150+
[go to top](#top)
151+
126152

127153
You can even declare variables as templates!
128154

@@ -138,7 +164,9 @@ pi<long>; // So on and so forth
138164

139165

140166

141-
### Multiple Arguments
167+
### 2.5 Multiple Arguments <a name="2.5"></a>
168+
[go to top](#top)
169+
142170

143171
You can pass multiple arguments to templates! Just add on more stuff.
144172

@@ -153,7 +181,9 @@ T maximise(T x, U y)
153181
154182
155183
156-
### Default Arguments
184+
### 2.6 Default Arguments <a name="2.6"></a>
185+
[go to top](#top)
186+
157187
158188
```c++
159189
template <typename T, typename U = int>
@@ -166,7 +196,9 @@ T maximise(T x, U y)
166196

167197

168198

169-
### Some Notes
199+
### 2.7 Some Notes <a name="2.7"></a>
200+
[go to top](#top)
201+
170202

171203
> **What is the difference between function overloading and templates?**
172204
> 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
182214
183215

184216

185-
## Template Metaprogramming
217+
## 3. Template Metaprogramming <a name="3"></a>
186218

187219
![giphy](assets/giphy.gif)
188220

@@ -194,7 +226,9 @@ But the main benefit of using templates to such a degree is greater generality o
194226

195227

196228

197-
### Template Parameters vs Template Arguments
229+
### 3.1 Template Parameters vs Template Arguments <a name="3.1"></a>
230+
[go to top](#top)
231+
198232

199233
> Example: `template <PARAMETER_LIST> struct <ARGUMENT_LIST>`
200234
@@ -209,7 +243,9 @@ void fun<int>(int a) // This second <> is known as the template argument list
209243

210244

211245

212-
### Template Specialisation
246+
### 3.2 Template Specialisation <a name="3.2"></a>
247+
[go to top](#top)
248+
213249

214250
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.
215251

@@ -306,7 +342,9 @@ public:
306342
307343
308344
309-
### Partial Specialisation
345+
### 3.3 Partial Specialisation <a name="3.3"></a>
346+
[go to top](#top)
347+
310348
311349
Partial specialisation works for structs and classes.
312350
@@ -359,7 +397,9 @@ int main() {
359397
360398
361399
362-
### Non-Type Template Arguments
400+
### 3.4 Non-Type Template Arguments <a name="3.4"></a>
401+
[go to top](#top)
402+
363403
364404
They're like another way to include arguments!
365405
@@ -387,7 +427,9 @@ arrMin<int, 100>(arr1, n1);
387427
388428
389429
390-
### Template Argument Deduction
430+
### 3.5 Template Argument Deduction <a name="3.5"></a>
431+
[go to top](#top)
432+
391433
392434
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!**
393435
@@ -412,7 +454,9 @@ If there is generally an exact match for the template arguments after passing th
412454
413455
414456
415-
### SFINAE (Substitution Failure Is Not An Error)
457+
### 3.6 SFINAE (Substitution Failure Is Not An Error) <a name="3.6"></a>
458+
[go to top](#top)
459+
416460
417461
![giphy](assets/giphy-1562828095877.gif)
418462

0 commit comments

Comments
 (0)