Chapter 9. Creating and Using Templates

Once a programming problem has been solved, you shouldn’t have to solve it again. One way to reuse code with different types is to write preprocessor macros, but that technique has drawbacks. It is not type-safe, meaning that there is no enforcement of correct type usage at compile time. A superior alternative is to use templates: generalized classes and functions that can be reused with different base types.

The best source of ready-made templates is the Standard Template Library (STL). Although not all of the earliest C++ compilers came with the STL, support for this template library is now standard.

This chapter is primarily about how to create your own templates. But even if you don’t create new templates, ...

Get C++ for the Impatient 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.