Template Functions

A stand-alone nonmember function can be defined as a template; the syntax is similar to the syntax of template class member functions.

template <class T>
void swap(T& x, T& y)
{ T a = x;  x = y;  y = a; }

When the function needs a prototype, it also contains the template parameter list with each class keyword followed by a parameter.

template <class T> void swap(T& x, T& y);

Both the definition and the prototype (forward declaration) start with the template keyword followed by the formal parameter list in angle brackets; each formal parameter consists of the keyword class followed by a programmer-defined identifier. The keyword class and the identifier are separated by commas. The identifier must occur only once in the parameter ...

Get Core C++ A Software Engineering Approach 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.