O'Reilly logo

Essential C++ by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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&, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required