|
Answer» You can use templates to:
1.Create a typesafe collection CLASS (for example, a stack) that can operate on data of any type.
2.Add extra type checking for functions that would otherwise take void pointers.
3.Encapsulate groups of operator OVERRIDES to modify type behavior (such as smart pointers).
4.Templates are easier to write. You create only one generic version of your class or function instead of manually creating specializations.
5.Templates can be easier to understand, SINCE they can provide a straightforward way of abstracting type information.
6.Templates are typesafe. Because the types that templates act upon are known at COMPILE time, the compiler can perform type checking before errors occur.
|