Skip to content

Commit

Permalink
Fix link and clarify template value parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael McLeod committed Nov 16, 2023
1 parent ead86db commit d733bae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 04cpp3/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This week we will look the role of pointers in C++, and some of the different op
- Smart pointers
- Data ownership and memory management
- Raw pointers
* [Templates](./sec03Templates)
* [Templates](./sec03Templates.html)
- Function templates
- Class templates
- Operator & function overloading
Expand Down
15 changes: 12 additions & 3 deletions 04cpp3/sec03Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ class myClassTemplate
- `T` is the template parameter, and the `typename` keyword tells us that `T` must denote a type. (You can equivalently use the `class` keyword.)
- Do note that you don't need to call your template parameter `T`; like function parameters or other variables, it can have any name. It's good to give it a more meaningful name if the type should represent something in particular, for example `matrixType` could be the name if your templated code deals with arbitrary types represnting matrices. This is especially useful when using templates with multiple template parameters!
- We can then use `T` like any other type inside the body of the class definition.
- Additional template parameters can appear in the angle brackets in a comma separated list e.g. `template<typename T1, typename T2>`.
- Template parameters do not have to be `typename`. You can also have a template parameter that is an `int`, or a `bool`, or any other type. These can be used to define special versions of classes with separate implementations when provided with particular values. For example we might have `template<int maxSize>` to define different classes depending on
the maximum size of data it will accept. This kind of template parameter is less common.
- Additional template parameters can appear in the angle brackets in a comma separated list e.g. `template<typename T1, typename T2>`. This is how e.g. `std::map` works.
**Template parameters do not have to be `typename`, i.e. we are not limited to simply templating types.** You can also have template parameters that are values such as an `int`, or a `bool`, or any other type. These can be used to define special versions of classes with separate implementations when provided with particular values. For example we might have `template<int maxSize>` to define different classes depending on the maximum size of data it will accept.
- `std::array` is a good example of a class template which take both a type and a value: the type of the elements and the number of elements in the array.
- Having values as template parameters means that they must be constants known at compile time.
- Using template parameters which are values can allow you to leverage to type system to enforce correctness on your program. For example, if your program models objects in 3D space, then you will need a representation of a 3-vector. If you use `std::vector<double>` then these vectors could be any size, so you have to make sure manually that no vectors of other sizes can sneak into your program. If you use `std::array<double, 3>` to represent a 3-vector then the compile will enforce that all positions, velocities, and so on are 3 dimensional. (If you work in general relativity, then this can also help you define different types for 3-vectors (`std::array<double, 3>`) and 4-vectors (`std::array<double, 4>`)!)
**N.B.** Templates which have many parameters (types or values) can make type names quite long, so if there is something that you want to use frequently you may consider giving it an alias using the `using` syntax:
```cpp
using Vec3 = std::array<double, 3>;
```
This can also make your type names more meaningful to people reading your code.

## Template Classes and Inheritance

Expand Down

0 comments on commit d733bae

Please sign in to comment.