© Ivor Horton and Peter Van Weert 2020
I. Horton, P. Van WeertBeginning C++20https://doi.org/10.1007/978-1-4842-5884-2_10

10. Function Templates

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

You may have noticed that some of the overloaded functions in Ex8_15 consisted of exactly the same code. The only difference were the types that appear in the parameter list. It seems an unnecessary overhead to have to write the same code over and over, just because it has to work for different types. 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++20: From Novice to Professional 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.