Chapter 11Writing Generic Code with Templates

  • How to write class templates
  • How the compiler processes templates
  • How to organize template source code
  • How to use non-type template parameters
  • How to write templates of individual class methods
  • How to write customizations of your class templates for specific types
  • How to combine templates and inheritance
  • How to write function templates
  • How to make function templates friends of class templates
  • How to write alias templates
  • Variable templates defined

Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/proc++3e on the Download Code tab.

C++ provides language support not only for object-oriented programming, but also for generic programming. As discussed in Chapter 6, the goal of generic programming is to write reusable code. The fundamental tools for generic programming in C++ are templates. Although not strictly an object-oriented feature, templates can be combined with object-oriented programming for powerful results. Many programmers consider templates to be the most difficult part of C++ and, for that reason, tend to avoid them.

This chapter provides the code details for fulfilling the design principle of generality discussed in Chapter 6, while Chapter 21 delves into some of the more advanced template features, including the following:

  • The three kinds of template parameters ...

Get Professional C++, 3rd 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.