© Ivor Horton and Peter Van Weert 2018
Ivor Horton and Peter Van WeertBeginning C++17https://doi.org/10.1007/978-1-4842-3366-5_9

9. Function Templates

Ivor Horton1  and Peter Van Weert2
Stratford-upon-Avon, Warwickshire, UK
Kessel-Lo, Belgium

In the section on overloading in the previous chapter, you may have noticed that some of the overloaded functions consisted of exactly the same code. The only difference is the types that appear in the parameter list. It seems an unnecessary overhead to have to write the same code over and over, and indeed it is. In such situations, you can write the code just once, as a function template. The Standard Library, for instance, makes heavy use of this feature to ensure that its functions work optimally with ...

Get Beginning C++17: From Novice to Professional now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.