Recitation - Apr 4, 2011
- Alows functions and classes to work with many different data types
without the programmer having to write a separate version for each type.
- For classes, it is a mechanism that describes how to create one
type given another type as a parameter.
- Arguments of a template do not need to be related by
inheritance.
- Common uses are container classes (e.g. List).
- Unlikely one would want just a list of a certain type.
- Templates allow the generalization of a list of a certain type (e.g. int) to a list of anything.
- template < class T >
- T is used as any other type name
- Scope of T extends to the end of the declaration it prefixes.
- Pair template example
- Template Instantiation class delcarlation from template
class and the template argument.
- Specialization version of a template corresponding
to a particular template argument.
- Class generated from templates is just like any other class
- No additional runtime mechanisms.
- No reduction in generated code.
- Standard template abstractions are commonly represented as
templates (e.g. string, ostream).
- Function Templates
- Resolving Ambiguities
- Explicit qualification
- Adding appropriate declarations
- Hints
- Write a concrete class/function without templates first. Then
once the concrete example is debugged generalize it into the templated
version.
- Other examples
Additional Notes