CHAPTER 27
Templates
Templates provide a way to make a class or function operate with different data types without having to rewrite the code for each type.
Function templates
The example below shows a function that swaps two integer arguments.
void swap(int& a, int& b){ int tmp = a; a = b; b = tmp;}
To convert this method into a function template that can work with any type the first step is to add a template parameter declaration before the function. This declaration includes the template keyword followed by the keyword class and the name of the template parameter, both enclosed between angle brackets. The name of the template parameter ...
Get C++ Quick Syntax Reference 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.