2.7. Defining and Using Template Functions

Let’s say that a colleague of ours has asked for three additional display_message() instances to handle a vector of integers, a vector of doubles, and a vector of strings:

void display_message( const string&, const vector<int>& ); 
void display_message( const string&, const vector<double>& ); 
void display_message( const string&, const vector<string>& ); 

On completing the implementation of these three instances, we notice that the function body for each instance is exactly the same. The only difference across these functions is the type of the second parameter: [3]

[3] A more flexible implementation adds a third parameter of type ostream that by default is set to cout:

 void display_message( const string&, ...

Get Essential C++ 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.