Chapter 20

Advanced Templates

WHAT’S IN THIS CHAPTER?

  • What the details are of the different kinds of template parameters
  • How to use partial specialization
  • How to write recursive templates
  • How to write type-safe variable arguments functions using variadic templates
  • What metaprogramming is and how to use it

The previous chapter covered the most widely used features of class and function templates. If you are interested in only a basic knowledge of templates so that you can better understand how the STL works, or perhaps write your own simple classes, you can skip this chapter on advanced templates. However, if templates interest you and you want to uncover their full power, continue reading this chapter to learn about some of the more obscure, but fascinating, details.

MORE ABOUT TEMPLATE PARAMETERS

There are actually three kinds of template parameters: type, non-type, and template template (no, you’re not seeing double: that really is the name). You’ve seen examples of type and non-type parameters in the previous chapter, but not template template parameters yet. There are also some tricky aspects to both type and non-type parameters that are not covered in the previous chapter.

More about Template Type Parameters

Type parameters to templates are the main purpose of templates. You can declare as many type parameters as you want. For example, you could add to the grid template a second type parameter specifying another templatized class container on which to build the grid. The ...

Get Professional C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.