[MUSIC] 1 2
So, currently, we've got a little bit of an issue when we spawn. 2 3
If you take a look at the code, very carefully, 3 4
you will find that we have this find empty location. 4 5
Which will find an empty location but it may not be empty when 5 6
at the end we've got all these empty locations and we start spawning into them. 6 7
That means that more than one empty location might actually 7 8
be able to overlap. 8 9
Now there's multiple ways you can solve this problem. 9 10
But we are going to go down a little bit of a diversion to learn 10 11
about a concept called template programming in C++. 11 12
And it's quite an important concept that's put pretty much unique to C++, so 12 13
you may not of heard about it before if you're coming from other languages. 13 14
And if you are coming from other languages similar or the most similar thing is 14 15
generics but it's not quite the same and we will see how in a minute. 15 16
So I'm actually gonna start off a new project to show us the basics of template 16 17
metaprogramming in a new environment, in a completely clean C++ environment. 17 18
So let's go ahead and create ourselves that new project. 18 19
I'm gonna go and do a new project, 19 20
go to Win 32 console application will do. 20 21
I am going to save my solution, yes. 21 22
And just go through the wizard, accepting the defaults for the most part. 22 23
You guys know the drill here. 23 24
Okay, so here is my console application one as it's been so amply named. 24 25
And what I want to show you is the basic premise of a template. 25 26
So, the idea is if I want to create 26 27
a GetMax function to get the maximum between two numbers. 27 28
And let's say it's gonna return int, so 28 29
we're going to GetMax, and it takes two ints and returns the highest of the two. 29 30
Int a and int b and 30 31
you can imagine the function is going to be fairly straight forward. 31 32
It's going to just be, 32 33
let's see if a greater than 33 34
b then we can return a. 34 35
That simple. Else, else, else, else return b, 35 36
obviously, cuz that's the max. 36 37
So that's a very simple GetMax function, and I can use it to GetMax, obviously. 37 38
So let's do a std::cout and 38 39
let's say do the GetMax of one and 39 40
two to get an idea for this. 40 41
And we'll do a std end line. 41 42
It's a very verbose syntax for doing this. 42 43
The little red squigglies here are obviously reminding me that I need 43 44
to include IO stream, so let's go ahead and do that. 44 45
And it's in the angle brackets I think Yes, excellent. 45 46
Let's go ahead and debug that. 46 47
Sorry, obviously I mean start without debugging, and there we go. 47 48
It worked successfully. 48 49
You got two. 49 50
Now, what happens if I want to write the very similar but 50 51
different 1.3 and 2.5? 51 52
How's that going to work? 52 53
Well if we go and start debugging. 53 54
There you go you'll notice it's not really working correctly cuz it's returning two. 54 55
What's actually happened here it's implicitly turned these floats into ints 55 56
because that's we're expecting. 56 57
But this function would work just as well if we have given it two ints. 57 58
So why, what two floats is what I mean. 58 59
So why can't we write the function like that? 59 60
What we'll have to do, sadly is copy this whole function and 60 61
just replace the ints with floats like so. 61 62
And now because they have different types signatures, 62 63
it should be able to pick up on the difference between the two. 63 64
In this case it's not, 64 65
because actually it's picked up these things as not being floats, but doubles. 65 66
So if I search put in double here instead 66 67
of floats then we shouldn't have it complaining. 67 68
Because what it's done is it's gone and matched the type of this GetMax. 68 69
And it's saying, well because I've got a version with double, 69 70
I'm going to use that. 70 71
But obviously, we hate code repetition like that and so did the creators of C++. 71 72
So, they said why when they're only changing the types 72 73
must we rewrite the whole function? 73 74
And what they came up with were templates and here's how you use them. 74 75
What you do is you write template before your function. 75 76
And then you use these Chevron's to denote the types that you want to be generic. 76 77
And we have to put in front of them class. 77 78
Just because we want to say that the type essentially 78 79
this variable is going to be a class name. 79 80
So c, t rather is going to be a class name. 80 81
And then what I can do is can replace double with t. 81 82
And this means that it'll work for int, it'll work for double. 82 83
It will even work for anything which supports this greater than operator. 83 84
How does that work on here you ask? 84 85
Well, it'll work by the fact that when you do a GetMax. 85 86
It essentially copies and pastes this code automatically for us, 86 87
like we just did manually but it does it automatically. 87 88
So I can do this and I can make it into floats by casting to float like this. 88 89
At the beginning of the number. 89 90
And we'll hopefully see that the return value will also be a float. 90 91
If we hover over our GetMax there you go, the return value is a float. 91 92
Here the return value is a double. 92 93
Hunky dory, and let's just make sure that we can still use ints as well. 93 94
When there are two different types, 94 95
it doesn't like it because they have to be the same type. 95 96
T is the same for a and b. 96 97
If I remove the dot from that, then we're going to get a return type of int. 97 98
Let's just run it for good measure. 98 99
And sure enough it's working just fine also. 99 100
Now what happens if I put in something that doesn't support our greater than. 100 101
So I don't know. 101 102
Let's do a string for example. 102 103
That's gonna be slightly weird [SOUND]. 103 104
I'm actually not sure what kind of arrow this is going to give so 104 105
this is an interesting one for me as well. 105 106
Hmm seems to work probably cause it thinks it's a CharStar and 106 107
it can compare to pointers. 107 108
So what we're going to get is the comparison of the two pointers. 108 109
It's just going to return the highest one. 109 110
But obviously, that's not exactly what you'd like. 110 111
It has returned one of them because it's compared the two pointers. 111 112
As I said, that's not exactly what we'd want. 112 113
Indeed, we'll find out how we can write a better version of GetMax later. 113 114
So my challenge to you is going to be to write a function which is a little bit 114 115
silly because obviously we could do a quiet easily already. 115 116
But we're gonna do a AddThree function, like so. 116 117
And it should be able to take a one, and a three object. 117 118
So the same type and add them all together and give you the result. 118 119
Pause the video and have a go at that. 119 120
Okay. 120 121
Welcome back. 121 122
So let's write this function. 122 123
It's going to be fairly straightforward as you might imagine. 123 124
We are going to make it a template just like so. 124 125
It's gonna be class and 125 126
then T which is just going to be the variable that's going to be replaced. 126 127
It doesn't have to be T, by the way. 127 128
So this could be type to add for example. 128 129
We're going to return that type to add. 129 130
We are going to call the method AddThree and 130 131
we are going to have three of these types to add. 131 132
So I am going to use T because it's just a bit shorter but we could not use T, 132 133
if we choose not to. 133 134
And we are going to have an a and Tb and a Tc. 134 135
Okay doke, and then we're going to do define the body, which as I said, 135 136
is going to be really simple, not very intelligent at all. 136 137
It's just gonna be a + b + c. 137 138
You never would have guessed. 138 139
There we go. 139 140
It's doing the AddThree and hopefully that should work. 140 141
Let's go and run, start without debugging. 141 142
And I would like to see the results of this. 142 143
Yep. Sure enough, we get six, 143 144
just as we would expect. 144 145
And let's just double check that this is going to work with floats as well. 145 146
So I'm going to put, in fact this is going to be doubles isn't it. 146 147
Cuz that's when I put these dots it's always going 147 148
to be doubles unless I put an f afterwards or I cast it to float. 148 149
I've got those numbers. 149 150
I am going to start with debugging like so. 150 151
Fair enough, it's added them up and 151 152
hopefully correctly you can get your calculators out and double check. 152 153
But that is the basics of templates. 153 154
It's as simple as that. 154 155
It's saying I don't want to have to specify all the types right here for you. 155 156
So what it's gonna do is is it's going to copy and 156 157
paste automatically these methods for us. 157 158
And there we go, that is C++'s little cleverness for 158 159
handling multiple types with the same code. 159 160
In the next lecture we will take a look at how you could handle multiple types 160 161
with slightly different code using something called template specialization. 161 162