C++: Templates
TEMPLATES
Templates is used to pass data type as
a parameter(arguments) so that we don’t need to
write same code for different data types.
Template is an example of compile time polymorphism
Declration of Templates:
template <typename T>
return_type function_name(arguments)
example:
A software company may need sort() for different
data types. Rather than writing and maintaining the
multiple codes, we can write one sort()
and pass data type as a parameter.
How templates works???...
Templates are expended at compiling time.
This is like macros. The difference is,
compiler does type checking before template expansion.
The idea is simple, source code contains only function/class,
but compiled code may contain multiple copies of
same function/class
example:
#include <iostream>
using namespace std;
template <typename T>
void fun(const T&x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}
int main()
{
fun<int> (1); //x=1 count=0
cout << endl;
fun<int>(1); //x=1 count=1
cout << endl;
fun<double>(1.1);//x=1.1 count=0
cout << endl;
return 0;
}
This is because there is diffrent funtion for doubled(data type)
which is created by compiler itself
Function Templates
function that can be used for different data types
This would work
even for user defined dattypes
#include <iostream>
using namespace std;
template <typename T>
T min(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << min<int>(3, 7) << endl; // Call min for int
cout << min<double>(3.0, 7.0) << endl; // call min for double
cout << min<char>('g', 'e') << endl; // call min for char
return 0;
}
O/P 3
3.0
e
Function Templates with multiple parameters
example:
template<class T1,class T2>
return_type function_name(T1 x,T2 y)
in this fuction we can pass two diffrent datatype
in arguments
void fun( 3,3.0)//one is int type and other is float type
Template Specialization
it is used
if we want a different code for a particular data type
example:
consider the following simple code
where we have general template fun() for all data types
except int.
For int, there is a specialized version of fun().
#include <iostream>
using namespace std;
template <class T>
void fun(T a)
{
cout << "The main template function " << a << endl;
}
template<>
void fun(int a)
{
cout << "Specialized Template for int type: " << a << endl;
}
int main()
{ outputs
fun<char>('a'); //The main template function a
fun<int>(10); //Specialized Template for int type
fun<float>(10.14); //The main template function 10.14
}
If a specialized version is present, compiler first
checks with the specialized version and then
the main template. Compiler first checks with the most
specialized version by matching the passed parameter
with the data type(s) specified in a specialized version.
Overloading of Template function
it is same as simple funtion overloading
example:
template<class T>
void fun(T x)
{ //code of this function};
template<class T, class T1)
void fun(T x,T1 y)
{//code of overloaded function};
Function Templates and static variables:
Each instantiation of function template has its own copy
of local static variables.
For example, in the following program
there are two instances: void fun(int ) and
void fun(double ).
So two copies of static variable i exist.
#include <iostream>
using namespace std;
template <typename T>
void fun(const T& x)
{
static int i = 10;
cout << "i="<<i;
return;
}
int main()
{
fun<int>(1); // i=10
cout << endl;
fun<int>(2); // i=11
cout << endl;
fun<double>(1.1); // i=10
cout << endl;
getchar();
return 0;
}
if it is not clear
please read How Templates work???...
Class Templates
Like function templates, class templates are useful
when a class defines something that is independent
of data type
example:
#include<iostream>
using namespace std;
template<class T, class U>
class A
{
T x;
U y;
Public:
A()
{
cout<<"I am here"<<endl;
}
};
int main()
{
A<char, char> a; //I am here
A<int, double> b; //I am here
return 0;
}
a & b is object name
it is also a example of class Templates with multiple parameters
Class Templates Specialization
#include <iostream>
using namespace std;
template <class T>
class Test
{
public:
Test()
{
cout << "General template object \n";
}
t
};
template <>
class Test <int>
{
public:
Test()
{
cout << "Specialized template object\n";
}
};
int main()
{
Test<int> a; //Specialized template object
Test<char> b; //General template object
Test<float> c; //General template object
return 0;
}
Class templates and static variables:
The rule for class templates is same as function templates
Each instantiation of class template has its own copy of member static variables. For example, in the following program there are two instances Test and Test. So two copies of static variable count exist.
#include <iostream>
using namespace std;
template <class T>
class Test
{
private:
T val;
public:
static int count;
Test()
{
count++;
}
};
template<class T>
int Test<T>::count = 0; //declration of static variable
int main()
{
Test<int> a;
Test<int> b;
Test<double> c;
cout << Test<int>::count << endl; // value of count is 2
cout << Test<double>::count << endl; //value of count is 1
getchar();
return 0;
}
if it is not clear
read How Templates Work
Templates is used to pass data type as
a parameter(arguments) so that we don’t need to
write same code for different data types.
Template is an example of compile time polymorphism
Declration of Templates:
template <typename T>
return_type function_name(arguments)
example:
A software company may need sort() for different
data types. Rather than writing and maintaining the
multiple codes, we can write one sort()
and pass data type as a parameter.
How templates works???...
Templates are expended at compiling time.
This is like macros. The difference is,
compiler does type checking before template expansion.
The idea is simple, source code contains only function/class,
but compiled code may contain multiple copies of
same function/class
example:
#include <iostream>
using namespace std;
template <typename T>
void fun(const T&x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}
int main()
{
fun<int> (1); //x=1 count=0
cout << endl;
fun<int>(1); //x=1 count=1
cout << endl;
fun<double>(1.1);//x=1.1 count=0
cout << endl;
return 0;
}
This is because there is diffrent funtion for doubled(data type)
which is created by compiler itself
Function Templates
function that can be used for different data types
This would work
even for user defined dattypes
#include <iostream>
using namespace std;
template <typename T>
T min(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << min<int>(3, 7) << endl; // Call min for int
cout << min<double>(3.0, 7.0) << endl; // call min for double
cout << min<char>('g', 'e') << endl; // call min for char
return 0;
}
O/P 3
3.0
e
Function Templates with multiple parameters
example:
template<class T1,class T2>
return_type function_name(T1 x,T2 y)
in this fuction we can pass two diffrent datatype
in arguments
void fun( 3,3.0)//one is int type and other is float type
Template Specialization
it is used
if we want a different code for a particular data type
example:
consider the following simple code
where we have general template fun() for all data types
except int.
For int, there is a specialized version of fun().
#include <iostream>
using namespace std;
template <class T>
void fun(T a)
{
cout << "The main template function " << a << endl;
}
template<>
void fun(int a)
{
cout << "Specialized Template for int type: " << a << endl;
}
int main()
{ outputs
fun<char>('a'); //The main template function a
fun<int>(10); //Specialized Template for int type
fun<float>(10.14); //The main template function 10.14
}
If a specialized version is present, compiler first
checks with the specialized version and then
the main template. Compiler first checks with the most
specialized version by matching the passed parameter
with the data type(s) specified in a specialized version.
Overloading of Template function
it is same as simple funtion overloading
example:
template<class T>
void fun(T x)
{ //code of this function};
template<class T, class T1)
void fun(T x,T1 y)
{//code of overloaded function};
Function Templates and static variables:
Each instantiation of function template has its own copy
of local static variables.
For example, in the following program
there are two instances: void fun(int ) and
void fun(double ).
So two copies of static variable i exist.
#include <iostream>
using namespace std;
template <typename T>
void fun(const T& x)
{
static int i = 10;
cout << "i="<<i;
return;
}
int main()
{
fun<int>(1); // i=10
cout << endl;
fun<int>(2); // i=11
cout << endl;
fun<double>(1.1); // i=10
cout << endl;
getchar();
return 0;
}
if it is not clear
please read How Templates work???...
Class Templates
Like function templates, class templates are useful
when a class defines something that is independent
of data type
example:
#include<iostream>
using namespace std;
template<class T, class U>
class A
{
T x;
U y;
Public:
A()
{
cout<<"I am here"<<endl;
}
};
int main()
{
A<char, char> a; //I am here
A<int, double> b; //I am here
return 0;
}
a & b is object name
it is also a example of class Templates with multiple parameters
Class Templates Specialization
#include <iostream>
using namespace std;
template <class T>
class Test
{
public:
Test()
{
cout << "General template object \n";
}
t
};
template <>
class Test <int>
{
public:
Test()
{
cout << "Specialized template object\n";
}
};
int main()
{
Test<int> a; //Specialized template object
Test<char> b; //General template object
Test<float> c; //General template object
return 0;
}
Class templates and static variables:
The rule for class templates is same as function templates
Each instantiation of class template has its own copy of member static variables. For example, in the following program there are two instances Test and Test. So two copies of static variable count exist.
#include <iostream>
using namespace std;
template <class T>
class Test
{
private:
T val;
public:
static int count;
Test()
{
count++;
}
};
template<class T>
int Test<T>::count = 0; //declration of static variable
int main()
{
Test<int> a;
Test<int> b;
Test<double> c;
cout << Test<int>::count << endl; // value of count is 2
cout << Test<double>::count << endl; //value of count is 1
getchar();
return 0;
}
if it is not clear
read How Templates Work
What if we pass different data types in arguments
ReplyDeleteSuch as
templates
Void fun(T x,T y)
If we pass
Fun(10,11.1)
Compiler error
ReplyDelete