18.1 Introduction

Templates have become extremely important in writing industrial-strength code. We’ve used templates in Chapters 6, 7 and 1317. In this chapter, we introduce some additional template capabilities and we continue using templates through the rest of the book. Figure 18.1 summarizes our template coverage.

Fig. 18.1 Summary of recursion examples and exercises in the text.

Location in text Discusses
Chapter 6  
Section 6.9 C++11 uniform_int_distribution class template for random-number generation.
Section 6.17 Defining a function template.
Chapter 7 Class templates array and vector for manipulating collections of elements.
Chapter 13  
Section 13.2 The stream-processing classes, which are class templates.
Chapter 14  
Section 14.2 The file-stream-processing classes, which are class templates.
Chapter 15 Standard Library collection class templates.
Chapter 16 Standard Library algorithm function templates.
Chapter 17  
Section 17.9 The unique_ptr smart-pointer class template for managing dynamically allocated memory.
Chapter 18 Creating custom class templates.
Chapter 19 Creating custom templatized data structures.
Chapter 20 Implementing searching and sorting algorithms as function templates.
Chapter 21  
Section 21.1 strings, which are class templates.
Section 21.12 string-stream class templates.
Chapter 24
Section 24.2 Smart pointer class templates.
Section 24.10 Variadic templates.
Section 24.11 Class template tuple.
Section 24.12 Class template initializer_list.

Function templates enable you to conveniently specify a variety of overloaded functions—called function-template specializations. Class templates enable you to conveniently specify a variety of related classes—called class-template specializations. Programming with templates is also known as generic programming. Function templates and class templates are like stencils out of which we trace shapes; function-template specializations and class-template specializations are like the separate tracings that all have the same shape, but could, for example, be drawn in different colors, line thicknesses and textures.

In this chapter, we demonstrate how to create a custom class template and a function template that manipulates objects of our class-template specializations. We focus on the template capabilities you’ll need to build the custom templatized data structures that we present in Chapter 19.1

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset