Skip to content

Commit d6204c6

Browse files
committed
Add clarification for C++ template parameter and arguments
1 parent 9beb42f commit d6204c6

File tree

1 file changed

+29
-60
lines changed

1 file changed

+29
-60
lines changed

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

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

1717

1818

19-
## Table Of Contents <a name="top"></a>
19+
## Table Of Contents
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)
3721

3822

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

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

4527
> **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.
4628
4729

4830

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)
31+
## C++ Templates Reference
5332

33+
### Template Concept
5434

5535
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.
5636

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

6343

6444

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

6947
Let's look at a simple example for defining function templates!
7048

@@ -94,9 +72,7 @@ T maximise(T x, T y)
9472
9573
9674
97-
### 2.3 Class Templates <a name="2.3"></a>
98-
[go to top](#top)
99-
75+
### Class Templates
10076
10177
You can template into classes too!
10278
@@ -146,9 +122,7 @@ int main() {
146122

147123

148124

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

153127
You can even declare variables as templates!
154128

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

165139

166140

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

171143
You can pass multiple arguments to templates! Just add on more stuff.
172144

@@ -181,9 +153,7 @@ T maximise(T x, U y)
181153
182154
183155
184-
### 2.6 Default Arguments <a name="2.6"></a>
185-
[go to top](#top)
186-
156+
### Default Arguments
187157
188158
```c++
189159
template <typename T, typename U = int>
@@ -196,9 +166,7 @@ T maximise(T x, U y)
196166

197167

198168

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

203171
> **What is the difference between function overloading and templates?**
204172
> 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
214182
215183

216184

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

219187
![giphy](assets/giphy.gif)
220188

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

227195

228196

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

233199
> Example: `template <PARAMETER_LIST> struct <ARGUMENT_LIST>`
234200
@@ -241,12 +207,23 @@ void fun<int>(int a) // This second <> is known as the template argument list
241207
}
242208
```
243209

210+
Template parameters are used to first decide which template to even use.
244211

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+
```
245222

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

249224

225+
### Template Specialisation
226+
250227
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.
251228

252229
**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:
342319
343320
344321
345-
### 3.3 Partial Specialisation <a name="3.3"></a>
346-
[go to top](#top)
347-
322+
### Partial Specialisation
348323
349324
Partial specialisation works for structs and classes.
350325
@@ -397,9 +372,7 @@ int main() {
397372
398373
399374
400-
### 3.4 Non-Type Template Arguments <a name="3.4"></a>
401-
[go to top](#top)
402-
375+
### Non-Type Template Arguments
403376
404377
They're like another way to include arguments!
405378
@@ -427,9 +400,7 @@ arrMin<int, 100>(arr1, n1);
427400
428401
429402
430-
### 3.5 Template Argument Deduction <a name="3.5"></a>
431-
[go to top](#top)
432-
403+
### Template Argument Deduction
433404
434405
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!**
435406
@@ -454,9 +425,7 @@ If there is generally an exact match for the template arguments after passing th
454425
455426
456427
457-
### 3.6 SFINAE (Substitution Failure Is Not An Error) <a name="3.6"></a>
458-
[go to top](#top)
459-
428+
### SFINAE (Substitution Failure Is Not An Error)
460429
461430
![giphy](assets/giphy-1562828095877.gif)
462431

0 commit comments

Comments
 (0)