Chapter 12 Templates 1. Templates enable us to specify a range of related overloaded functions -- called template functions -- or a range of related classes -- called template classes. 2. To use function templates, the programmer writes a single function template definition. Based on the argument types provided in calls to this function, C++ automatically generates separate functions to handle each type of call appropriately. These are compiled along with the rest of the program's source code. 3. All function template definition begin with the keyword template followed by formal parameters to the function template enclosed in angle brackets (<>); each formal parameter must be preceded by the keyword class (or the keyword typename). Keyword class used to specify function template type parameters means "any built-in type or user-defined type". 4. Template definition formal parameters are used to specify the types of the arguments to the function, the return type of the function, and to declare variables in the function. 5. The name of a formal parameter can be used only once in the parameter list of a template header. Formal parameter names among template functions need not be unique. 6. A function template itself may be overloaded in several ways. We can provide other function templates that specify the same function name but different function parameters. A function template can also be overloaded by providing other non-template functions with the same function name but different function parameters. 7. Class templates provide the means for describing a class generically and instantiating classes that are type-specific versions of this generic class. 8. Class templates are called parameterized types; they require type parameters to specify how to customize a generic class template to form a specific template class. 9. The programmer who wishes to use template classes writes one class template. When the programmer needs a new type-specific class, the programmer uses a concise notation and the compiler writes the source code for the template class. 10. A class template definition looks like a conventional class definition except that it is preceded by template < class T> to indicate this is a class template definition with type parameter T indicating the type of the class to be created. The type T is mentioned throughout the class header and member function definitions as a generic type name. 11. The member function definitions outside the class template header each begin with the header template . Then each function definition resembles a conventional function except that the generic data on the class is always listed generically as type parameter t. the binary scope resolution operator is used with the class template name to tie each member function definition to the class template class template's scope as in Classname . 12. It is possible to use non-type parameters in the header of a class template. 13. A calss for a specific type can be provided to override the class template for that type. 14. A calss template can be derived from a template class. A class template can be derived from a non-template class. A template class can be derived froma class template. A non-class template class can be derived from a class template. 15. Each template class instantiated from a class template has its own copy of each static data member of the class template; all objects of that template class share that one static data member. And as with static data members of non-template classes, static data members of template classes must be initialized at file scope. 16. Each template class gets a copy of the class template's static member functions. 17. Template certainly offer the benefits of software reusability. But keep in mind that multiple copies of template functions are still instantiated in a program despite the fact that the template is written only once. These copies can consume considerable memory. 18. Function templates, like macros, enable software reuse. But, unlike macros, function templates help eliminate many type of errors because of the scrutiny of full C++ type checking.