7/30/2019 Templates C++
1/19
Templates(using C++)
7/30/2019 Templates C++
2/19
Introduction
Templates are part of ANSI C++ Standard.
Template is a technique that allows using a single
function or class to work with different data types.
A single function is created to process any type of data
with in template.
2
7/30/2019 Templates C++
3/19
Introductioncontd.
The template provides generic programming by defining
generic classes.
In Templates, generic data types are used as arguments and
they can handle a variety of data types.
A function that works for all C++ Data types is called a
generic function.
The formal arguments of template functions are of
template(generic) type.
3
7/30/2019 Templates C++
4/19
Types of Templates
FunctionTemplates
Class
Templates
7/30/2019 Templates C++
5/19
C++ Function Templates
Approaches for functions that implement identical
tasks for different data types
Nave Approach
Function Overloading Function Template
Instantiating a Function Templates
7/30/2019 Templates C++
6/19
Approach 1: Nave Approach
create unique functions with uniquenames for each combination of data
types difficult to keeping track of multiple
function names
lead to programming errors
7/30/2019 Templates C++
7/19
Examplevoid PrintInt( int n )
{ cout
7/30/2019 Templates C++
8/19
Approach 2:Function Overloading
(Review) The use of the same name for different C++
functions, distinguished from each other bytheir parameter lists
Eliminates need to come up with many different names foridentical tasks.
Reduces the chance of unexpected results caused byusing the wrong function name.
7/30/2019 Templates C++
9/19
Example of Function Overloading
voidPrint( int n ){
cout
7/30/2019 Templates C++
10/19
Approach 3: Function Template
A C++ language construct that allows the compiler to generatemultiple versions of a function by allowing parameterized data types.
Template < TemplateParamList >FunctionDefinition
FunctionTemplate
TemplateParamDeclaration: placeholder
class typeIdentifiertypename variableIdentifier
7/30/2019 Templates C++
11/19
1. The first statement tells the compiler that the following function or class
declaration can use the template data type.
2. Templates can not be declared inside classes or functions.
3. They must be global and should not be local.
Template //Template declaration
Class cname
{// data members and member functions
}
7/30/2019 Templates C++
12/19
Example of a Function Template
template
void Print( SomeType val ){
cout
7/30/2019 Templates C++
13/19
Instantiating a Function Template
When the compiler instantiates a template,it substitutes the template argument for thetemplate parameter throughout the functiontemplate.
Function < TemplateArgList > (FunctionArgList)
TemplateFunction Call
7/30/2019 Templates C++
14/19
Summary of Three Approaches
Nave ApproachDifferent Function Definitions
Different Function Names
Function OverloadingDifferent Function Definitions
Same Function Name
Template Functions
One Function Definition (a function template)Compiler Generates Individual Functions
7/30/2019 Templates C++
15/19
#include
#include
template
void show(SS tv)
{
cout
7/30/2019 Templates C++
16/19
Class Template
A C++ language construct that allows the compiler to generatemultiple versions of a class by allowing parameterized data types.
Template < TemplateParamList >ClassDefinition
Class Template
TemplateParamDeclaration: placeholder
class typeIdentifiertypename variableIdentifier
7/30/2019 Templates C++
17/19
Instantiating a Class Template
Class template arguments mustbe explicit.
The compiler generates distinct class typescalled template classes or generated classes.
When instantiating a template, a compilersubstitutes the template argument for thetemplate parameter throughout the class
template.
7/30/2019 Templates C++
18/19
7/30/2019 Templates C++
19/19
#include
#include
template
class demo
{
public:
demo(T tvar, T1 tv)
{
cout