- [Instructor] C++ supports both template functions
and template classes.
In this movie, we'll be looking at template functions.
Here is template-function.cpp from chapter seven
of the exercise files.
This is a simple template function that returns
the maximum value of two arguments.
The part in the angle brackets
is called the template argument list.
The keyword type name is commonly used here
but the keyword class
also works and is often used.
Just be aware that at least in this context
either of those tokens means the same thing.
I strongly recommend that you use type name.
You may see class used in legacy code
but type name is less ambiguous.
This simple template is both type safe and type agnostic.
It's type agnostic because all work for any class or type
that supports the greater-than operator for comparisons.
It's type safe because the compiler will generate
a specialization of the function
for each given type that uses this function.
It's important to note that you cannot define a template
within a block.
It must be outside of any blocks in your code.
So down here in our main, we can do this,
and you notice we have maxof with int in angle brackets
showing that this is a specialization of this template
for the type int.
And it'll compare these two integers,
seven and nine and return the greater of them.
When I build and run, you notice it returns a nine.
In order to do this, the compiler generates a version
of the function that operates on int and returns int.
This generated version is called a specialization.
You never see it, but you need to know that it's there
and that's how templates work.
On the other hand, we can do this,
and I'll build and run,
and in this case, the compiler generates a specialization
of the function that operates on C-strings
and returns the C string,
which may not give the results that you expect.
You'll notice here that this version returned aaa
which is not, alphabetically at least,
the greater of the two strings.
Your results may differ because what it's comparing here
is not the strings, but rather the pointers.
On the other hand, we can specify that we want to use
the std string class so I can come up here and include that.
And now I can compare those,
and now when I build and run, it's comparing std strings
and I get the greater of the two strings alphabetically.
Notice that in all of this,
I have never touched the template function
for all of these different examples,
and this is the beauty of templates.
It's also possible to call this function
without specifying the type of the argument at all.
So I can take out all of that in the angle brackets
and I can just say,
seven and nine, and when I build and run you'll notice
that it returns a nine.
This is a feature called template argument deduction.
In a nutshell if the compiler can easily deduce
the type of the argument,
it will call the appropriate specialization without need
to explicitly specify the type.
This is very convenient
and it works in many common circumstances,
although, it's worth testing your code
if you're going to use template argument deduction
because it may not always give you the results
that you expect.
Template functions are a form of generic programming
that are easy to create and can be very useful
for many of the purposes that we used to use C macros.
C++ template functions are more powerful or flexible
and less prone to error.