O'Reilly logo

C++ for the Impatient by Brian Overland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required