INFO
Allows creation of a single function that can operate with different data types. This is achieved by defining a template that specifies the function’s behavior without committing to a specific data types
Syntax
template <template-parameters> function-declaration
- Template Parameters: A series of parameters separated by commas that can be generic template types by specifying either the
classortypenamekeyword followed by an identifier.
NOTE
classis the old way to use function templates. Recommend usingtypenameinstead ✓
- Identifier: A “variable class” that can be used in the generic function declaration as if it was a regular type
template <typename T>
T sum(T a, T b){
return a + b;
}- Declaring
T(a generic type within the template parameters enclosed in <>) allowsTto be used anywhere in the function definition → just as any other type- Represents a generic type that will be determined on the moment the template is instantiated
- Instantiating a template is applying the template to create a function using particular types or values for its template parameters
- Same syntax as calling a regular function but specifying the template arguments enclosed in <>
name <template-arguments(function-arguments)
int x = sum<int>(10, 20);Class Templates
Generic Class
- all instance of the
typenamein class definition will be replaced by the type assigned by parameter - Once template class is defined, we can declare objects of the class by using the same syntax used in STL classes → enclosing types with <>
Template and Inheritance
- Can be safely inherited
- When
base class == template, derived class inherits the sametypename
Note
- Each function definition is preceded by
template <typename T>- Each function definition is itself a template
- Class qualifier1 is now
ClassName<T>instead of simplyClassName
Compiler Complication
- Most compilers do not support templates
- will not make connections between
- function declaration ⇆ function definition
- class interface ⇆ class implementation
- will not make connections between
- Possible Solutions
- for non-member functions, use
template<typename T>- before function declaration
- before function definition
- for class, check compiler specific requirements
- Some need specific options set
- Some require special order of arrangement of template definitions or other file items
- for non-member functions, use
- Class templates as parameters
- Passing object of template class as parameter
- need to include type in between <>
- Passing object of template class as parameter
void print(const std::className<type>& param);Common Errors and Strategies
Common Errors
- Using mixed types of parameter with same identifiers
swapValues(const T& param1, const T& param2); // ✓
swapValues(int param1, double param2); // ✗- Forgetting to include .cpp file when template class is used
# include "Template.h"
# include "Template.cpp" // need to include this- Forgetting member functions definitions are themselves template and must be preceded by
template <typename T>
template <typename T>
class<T>::function(const T& param){...}Strategies
- Implement functions or classes normally with any datatype
- Debug functions or classes
- Change to a template by replacing
typeswithtypename- Only replace the types that need to be replaced
- Add
<T>next to class qualifier1