Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Generic programming – Function template

Conventional Approach
void printInt( int n )
{
cout << “The Value is " << n ;
}
void printChar( char ch )
{
cout << “The Value is " << ch ;
}
void printFloat( float x )
{
cout<<“The value is”<<x;
}
void printDouble( double d )
{
cout<<“The value is”<<d;
}
Function overloading Approach
void Print( int n )
{
cout << “The Value is " << n ;
}
void Print( char ch )
{
cout << “The Value is " << ch ;
}
void Print( float x )
{
cout<<“The value is”<<x;
}
void Print( double d )
{
cout<<“The value is”<<d;
}
Generic function Approach
 
template<class SomeType> Template parameter
(class, user defined type,
void Print( SomeType val ) built-in types)
{
cout << “The value is " << val ;
}
Approach-1(Conventional)

• Create separate functions with unique names for each combination of

data types

– Difficult to keep track of multiple function names

Approach-2(Function overloading)

• Eliminates the need to come up with many different names for identical

tasks.

Approach-3(Generic function)

• When the compiler instantiates a template, it substitutes the template

argument for the template parameter throughout the function


Summary of Three Approaches

Conventional Approach Function Overloading


Different Function Definitions Different Function Definitions
Different Function Names Same Function Name

Template Functions
One Function Definition (a function template)
Compiler Generates Individual Functions
How to perform the swapping on different data
items?
• The above program performs well and is
producing the output as well.
• But it is less efficient. Because the same
function “swap” is redefined by multiple
times.
• How to increase the efficiency of the above
program?
Templates
• Template is a kind of macro that supports the
generic programming which allows to develop the
reusable components.
• It is one of the main features of object oriented
language such as C++.
• Actually, it allows the declaration of data items
without specifying their exact data type. Two types:
1.Function template

2.Class template
Need of Function Templates
• Reusability

• Avoids the redefinition of function.

• Describes a function format that when


instantiated with particulars generates a
function definition.
• Write once, use multiple times
Function Templates
• Function templates are generic functions, which
can operate on different data items.
• C++ allows compiler to generate multiple
versions of a function by allowing parameterized
data types and hence it supports the
parameterized polymorphism.
• Data items are not declared while defining
function
• When the function call is made using appropriate
data type, then the template function is
transformed to operate on that specific data
types
• For example, if we want to invoke the function
swap(), so we just supply the required data
type parameters to it and the compiler
automatically invokes the function template
by defining the appropriate data items.
• Therefore it allows a single “swap” with
generic data type “T” to deal with multiple
swap functions.
General syntax for function template:
• The syntax of the function template is similar
to normal function definition

template<class type>
returntype functionname(arguments)
{ …………..
statements;
……….
}
Guidelines to use the function
template
1. No argument template function
template<class T>
T fun() //Error: T is not used as an argument
{
return n;
}
2. Template type argument unused
template<class T>
void test(int x) //Error: T is not used as an argument
{
T temp;
------
------
}
3.Usage of partial number of template
arguments
template<class T, class U>
void fun(T &x) //Error: U is not used as an argument
{
U temp;
------
------
}
Example Program
Overloaded Function Templates
• The function template can also be overloaded
with multiple declarations.
• As similar to function overloading, the
overloaded function templates must differ
interms of number of parameters or their
type.
• Declaration of a function template for
functions having multiple parameters of
different types requires multiple generic
arguments or template arguments
Example Program
How to use multiple parameters in
function template??
•In main(), the statement
assign_B(50,10.15,s2); leads to compilation
errors.
•Since, the above program is neither having the
normal function nor function template
matching with its parameters data type.
•Therefore, the solution to the problem
encountered in the above program is to declare
the second template function in the above
program as follows:
How to use multiple parameters in
function template??

•The declaration of the function template is the


same, except that it has an extra argument in the
template-argument-list, i.e class U.
•This declaration informs the compiler that
template function assign_B() with two arguments
should be instantiated.
•The compiler calls the appropriate instantiation.
Any number of generic data types can be
declared, provided all these generic data types
are used in declaring formal parameters.
How to call the function template
using user defined type
In Lab Practice problem
• In a library, the books are arranged vertically
in the rack, one above the other. The book(s)
can be added or removed only from the top
and not in the middle. You have been assigned
to add 10 books and remove the books until
the book rack is empty. Develop the program
using generic function.

You might also like