Learn about the template, a compiler abstraction that allows you to write generic code that applies to different types and classes.
- [Instructor] In order to fully understand the Standard Template Library, you must first understand templates. For a brief introduction to templates, please see the companion course C++ Essential Training. This chapter will give you a more detailed understanding. Here I have a working copy of template-function.cpp from Chapter One of the exercise files. A template is essentially a compiler abstraction that allows you to write generic code that applies to various types or classes without concern for the details of the type.
In this example, a template function, when you call the function and here's the template function here, when you call the function a specialization is created. So let's go ahead and build and run this and we'll talk about it. This template function is called maxof. The abstraction is the type T, and so it doesn't know about the type, and at this point we don't want to necessarily think about the type, we just want to think about the logic of the function.
And so we have two arguments, a and b, and they're both of type constant T. If a is greater than b, we will return a, and if not then we will return b. And so here we've declared two integers, and our template, we're giving it the type int, inside the template parameters. And we pass it this a and b which are integers, and we get the result of nine. So when the compiler encounters this template, it says okay and it stores it away, and when it encounters the implementation here, where we call it, it says okay I need a specialization with int in place of T.
And then it will create a function called int maxof, const, int, a, const, int, b. Like that. And now if we comment this out, obviously. Oh, need to take out this template parameter there. And we build and run, we see it works exactly as expected.
And so when we have the template function, the compiler it encounters all of this, and it says okay I need a specialization of this template function with int as the type, and it builds this on its own. It built that int specialization. And so if instead of int I were to say const char pointer. Like this. And inside of the template parameter, I put const char like that.
And now obviously instead of the seven, I type seven. And instead of the nine, I type nine in quotes. Now when I build and run, you notice we get the result nine. Well alphabetically seven is greater than nine, And so really what we would expect here is a seven, not a nine. What's happened is our template specialization is built for the constant character pointer and it's comparing the pointers. It's not comparing what's pointed at.
It's not comparing the strings. And if we think about it, that's really what we would expect. So instead if we say string, and you notice I have the string header already included up here, and string here and in our template parameter, we put string. Now we're going to get an alphabetical comparison. Oops, I left that pointer in. Now we build and run. We get seven, which in alphabetical comparison, seven is greater than nine.
And so a specialization was built for each of these when it was compiled. In this case the specialization of string, well the string type it knows about the comparison operators. And it's going to compare them based on the strings. The pointer type will use the comparison operator and compare the pointers, which is what we got and maybe what we should've expected, but might not have been what we initially expected. So to recap, a template is a compiler abstraction. We'll get into details throughout this chapter.
But the compiler creates a specialization for each type, or each combination of types passed to the template. This is important understand as we move on to the details in the rest of this chapter.
- Template fundamentals
- STL containers
- Accessing iterators
- Output, forward, and bidirectional iterators
- The transform function
- Transforming strings and types
- Arithmetic, relational, and logical functions
- STL algorithms