Declaring and Using Templates

You've seen a template in use before—std::vector<aRequest> myTape; in aTape, from Lesson 20, “The Rest of the Calculator as Classes.” This member variable declaration created an instance of vector that would only accept aRequest objects.

It can help to think of such a template as a form that will generate code for a class when you “fill in the blanks.”

Templates usually have only one or at most a few such “blanks,” which are called type parameters. A value supplied for the type parameter is used to replace that type parameter wherever it is mentioned in the template. In std::vector<aRequest>, aRequest is the value supplied for the type parameter of the vector template.

The format for coding a template can seem complex ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION 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.