O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix A
C++ Templates
T
his appendix has been written as a quick and very brief introduction to
C++ templates. It only skims the surface of what you can achieve with
templates, but I cover enough ground for you to understand the code that
accompanies this book.
Function Templates
If you have never used templates before then you will have almost cer-
tainly at some time created several versions of the same function to cater to
each type you require the function to operate on. For example, you may
create a function called
Clamp that takes an integer and makes sure its value
is between two limits. Something like this:
void Clamp(int& Val, int MinVal, int MaxVal)
{
if (Val < MinVal) {Val = MinVal; return;}
if (Val > MaxVal) {Val = MaxVal; return;}
}
Later on in your project, you realize that you want the same functionality
but for floats. So you create another version of
Clamp:
void Clamp(float& Val, float MinVal, float MaxVal)
{
if (Val < MinVal) {Val = MinVal; return;}
if (Val > MaxVal) {Val = MaxVal; return;}
}
And you may find that you want to add more versions for other types too.
Repeating all this code every time you want to support a new type can
become a pain in the butt. Fortunately for us, C++ templates provide a
mechanism to allow classes and functions to be parameterized so that they
provide the same behavior for different types. The declaration of a function
template is very similar to a normal function declaration except the type is
left unspecified. Here’s how the
Clamp function shown previously looks as
a function template:
template <typename T>
void Clamp(T& Val, T MinVal, T MaxVal)
{
if (Val < MinVal) {Val = MinVal; return;}
459
if (Val > MaxVal) {Val = MaxVal; return;}
}
The template keyword is basically stating that this definition specifies a
family of functions that are parameterized by the template parameter
T.
When this function template is called, an instance of
Clamp will be gener
-
ated by the compiler for each type for which the template is used. So, given
the following program:
int main()
{
int intVal = 10;
int iMin = 20;
int iMax = 30;
cout < "\nintVal before Clamp="<intVal;
Clamp(intVal, iMin, iMax);
cout < "\nintVal after Clamp="<intVal;
float floatVal = 10.5;
float fMin = 25.5;
float fMax = 35.5;
cout < "\n\nfloatVal before Clamp="<floatVal;
Clamp(floatVal, fMin, fMax);
cout < "\nfloatVal after Clamp="<floatVal;
return 0;
}
the compiler will create two instances of Clamp, one taking integers as
parameters and the other taking floats, giving the output:
intVal before Clamp = 10
intVal after Clamp = 20
floatVal before Clamp = 10.5
floatVal after Clamp = 25.5
Adding More Types
So far so good, but what if you wanted to do something like:
Clamp(floatVal, iMin, iMax);
Now that the parameters include one float and two integers, the function
template for
Clamp given previously will not compile. You will get the
error: “template parameter 'T' is ambiguous” or something similar. To pro
-
vide support for more types you must add them to the parameterized list
like so:
460 | Appendix A
Function Templates
template <typename T, typename M>
void Clamp(T& Val, M MinVal, M MaxVal)
{
if (Val < MinVal) {Val = MinVal; return;}
if (Val > MaxVal) {Val = MaxVal; return;}
}
Using this definition the Clamp family of functions will accept different
types for the value and for the range (a little contrived I know, but it gets
the point across). Now you can do stuff like this:
int main()
{
int intVal = 10;
int iMin = 20;
int iMax = 30;
float floatVal = 10.5;
float fMin = 25.5;
float fMax = 35.5;
cout < "\nintVal before Clamp="<intVal;
Clamp(intVal, fMin, fMax);
cout < "\nintVal after Clamp="<intVal;
cout < "\n\nfloatVal before Clamp="<floatVal;
Clamp(floatVal, iMin, iMax);
cout < "\nfloatVal after Clamp="<floatVal;
return 0;
}
giving the output:
intVal before Clamp = 10
intVal after Clamp = 25
floatVal before Clamp = 10.5
floatVal after Clamp = 20
Class Templates
Just like functions, classes can be parameterized with one or more types.
For example, the STL container classes you are probably familiar with are
class templates you can use to manipulate collections of objects, whatever
their type.
A common way of demonstrating how a class template works is to
implement a stack-like data structure. Here’s the declaration of
Stack,a
class that acts as a stack and can hold five elements of the parameterized
type
T:
template <class T>
class Stack
{
private:
C++ Templates | 461
Class Templates

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required