Chapter 1

Function Templates

This chapter introduces function templates. Function templates are functions that are parameterized so that they represent a family of functions.

1.1 A First Look at Function Templates

Function templates provide a functional behavior that can be called for different types. In other words, a function template represents a family of functions. The representation looks a lot like an ordinary function, except that some elements of the function are left undetermined: These elements are parameterized. To illustrate, let’s look at a simple example.

1.1.1 Defining the Template

The following is a function template that returns the maximum of two values:

basics/max1.hpp

template<typename T> T max ...

Get C++ Templates: The Complete Guide, 2nd 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.