Generic Functions

Code to do things like search a list, copy values, and so forth will often look exactly the same, no matter what types are being used. The standard algorithms, which you first met in Chapter 3, “Arrays and Algorithms,” are examples of what is sometimes called generic programming. Rather than write out the same loops, you can reuse the generic code. In this section you will see exactly how such functions are generated, using function templates.

sqr() Revisited

The first example of overloading that we examined in Chapter 6, “Overloading Functions and Operators,” involved a pair of functions that were both called sqr() but dealt with int and double arguments. Any type that can be multiplied—not only ordinary types, but mathematical ...

Get C++ By Example: UnderC Learning 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.