C++ Programming

C++ Template Function & how to use a template in C++ programming, with examples

What is C++ template programming?

C++ template are the foundation of generic programming, so what exactly generic programming is it involves writing code in a way that is independent of any particular type and by type I mean the data type so you will get a very clear idea when we actually see a program. So C++ template is a blueprint or a formula for creating a generic class or a function. To perform identical operations for each type of data compactly and conveniently, use function templates. You can write a single function template definition. Based on the argument types provided in calls to the function, the compiler automatically instantiates separate object code functions to handle each type of call appropriately. The STL algorithms are implemented as function templates.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

The general syntax of c++ template:

template <class T>

T f-name (T argument (s))

{

statement(s);

}

Template:

keyword template is used to define the function template.

Class:

In a function template a general data name is defined by using the keyword “class” In the above syntax, it is represented by the word “T”. Any character or word can be used after the keyword “class” to specify the general data name.

f-name:

It specifies the name of the function.

 argument(s) :

These are the arguments that are to be passed to the function. The arguments are declared of type T. As explained earlier, the type T specifies any standard data type.

statement(s):

It specifies the actual statements of the function.

 For example, to write a function to calculate a sum of three value either integers or floating-points, the function template is written as:

template <class T>

T sum (T a, T b, T c)

{

return a+b+c;

}



Types of  c++ template :

there are two different types of  c++ templates

  • function template
  • class template

in this tutorial will be just focusing on function template and we will also see a program

 what is function templates in C++

function templates are special functions that can operate with generic types, now generic type as we just saw other type which do not have a predefined data type, by creating the  function template it allows us to use the functionality of more than one type or class without repeating the entire code for each type. Now, what exactly does this mean is that the simple idea is to pass data type as a parameter just as we pass variables as parameters we can pass data type as parameter so then we don’t need to write the same code for different data types and this essentially gives us the polymorphic kind of functionality and function templates. Now we write the generic function that can be used for different data types so what exactly does this look in terms of programming aspect.

Example: how to write a program which sum two integer number  using  c++ template:

#include<iostream>
Using namespace std;
Template <typemane T>
T add(T x, T y)
{
Return(x+y);
}
Int main()
{
Cout<<”sum of 5 and 5 Is:”<<add<int>(5,5)<<endl;
}

output:
sum of 5 and 5 Is:10


Programming Explanation:

As you can see the program is very simple. As this is a C++ program so that’s why I started of by adding the iostream header file.  I created a function template for the add function which is a user defined function. So here’s how the same tags goes, you have to first write down the keyword Template followed by another keyword in the angular brackets typename this is another keyword and this is supposed to be as it is you can give a space between them and it doesn’t matter if you have a space or not and then I created a placeholder, so typename T, now below that I created that function T add.  Inside this function I passed T x, T y.

The variables x and y are the arguments of the type T. Inside the add function we have only on instruction which is the

return(x+y);

As I said,  x and y are the parameters being passed and their data type and the  placeholder is T and the return type is also T, so the T will be resolved when we actually go out in the int main function and call it.

int main()

{

cout<<”sum of 5 and 5 Is:”<<add<int>(5,5)<<endl;

}

So inside the main function let’s says I want to add two integers 5 and 5. I simply, started by typing the cout<<”sum of 5 and 5 Is:” this is a string message which is displayed, this string message is followed by the add function and one more piece is added in the angular brackets and inside this I have passed int, so when calling a function template, the type of  the function we have to include this extra bit of information and it is essentially what the type T is going to be; so when I say int the T automatically becomes int, and the data type of the parameter also become int “integer”.

Copy the above program, Save the program, compile the program and finally, run the program. You should be able to see

Sum of 5 and 5 Is: 10

As the output which means the function template was successfully created and it ran without any errors

Example: how to write a program which sums two integers and two float number using  c++ template:

#include<iostream>
Using namespace std;
Template <typemane T>
T add(T x, T y)
{
Return(x+y);
}
Int main()
{
Cout<<”sum of 5 and 5 Is:”<<add<int>(5,4)<<endl;
Cout<<”sum of two float integer number 3.3 and 2.2 Is:”<<add<float>(3.3f,2.2f)<<endl;
}

The output of the program:
sum of 5 and 5 Is:10
sum of two float integer number 3.3 and 2.2 Is:5.5


C++ template programming Explanation:

As you can see this is the same exact program as I explained earlier. This time I did a little modification. I added another cout statement.

cout<<”sum of two float numbers 3.3 and 2.2 Is:”<<add<float>(3.3f,2.2f)<<endl;

This time the string message is the sum of two float numbers 3.3 and 2.2 Is:

This time I will be adding two float numbers 3.3 and 2.2. The string message is followed by the add function which is followed by the angular brackets, and this time inside the angular brackets I wrote float and finally, I passed parameters 3.3f and 2.2f. If you don’t know what this f ?

f stands for float or whenever you’re passing a float variable in argument you can explicitly tell that it is a float variable otherwise it takes double value so just save this and execute.

Example: how to write a program which sums two integers,  two float and two double numbers using  c++ template:

#include<iostream>
Using namespace std;
Template <typemane T>
T add(T x, T y)
{
Return(x+y);
}
Int main()
{
Cout<<”sum of 5 and 5 Is:”<<add<int>(5,5)<<endl;
Cout<<”sum of two float integer number 3.3 and 2.2 Is:”<<add<float>(3.3f,2.2f)<<endl;
Cout<<”sum of the double number 6.36 and 20.26 Is:”<<add<double>(6.36,20.26)<<endl;

}

The output of the program:
sum of 5 and 5 Is:10
sum of two float integer number 3.3 and 2.2 Is:5.5
sum of the double number 6.36 and 20.26 Is: 26.62


C++ template Programming Explanation:

Now let’s try to again replicate cout statement and try to add two double variables. So, I will say 6.36 and 20.26, and in the angular brackets I’ll pass the data type which is double. So as you can see in the program given above, we are passing the data type as well as an argument, but it is passed in the angular brackets, in double data type there is no need to write f. This time you can see there is no f with 6.36 and 20.26.

Example: Write a program to find the Maximum number using the C++ template:

#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Output of the above program:
6
10

Programming Explanation:

In the example program above, we used the function template GetMax() twice. The first time with arguments of type int and the second one with arguments of type long. The compiler has instantiated and then called each time the appropriate version of the function. As you can see, the type T is used within the GetMax() template function even to declare new objects of that type:

T result;

Therefore, result will be an object of the same type as the parameters a and b when the function template is instantiated with a specific type.

In this specific case where the generic type T is used as a parameter for GetMax the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like we have done before specifying <int> and <long>). So we could have written instead:

int i,j;

GetMax (i,j);

Since both i and j are of type int, and the compiler can automatically find out that the template parameter can only be int. This implicit method produces exactly the same result



Template Instantiation:

When the compiler generates a class, function or static data members from a template, it is referred to as template instantiation.

  • A class generated from a class template is called a generated class.
  • A function generated from a function template is called a generated function.
  • A static data member generated from a static data member template is called a generated static data member.

The compiler generates a class, function or static data members from a template when it sees an implicit instantiation or an explicit instantiation of the template.

  1. Consider the following sample. This is an example of implicit instantiation of a class template.
template <class T>
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z<int> zi ;   //implicit instantiation generates class Z<int>
Z<float> zf ; //implicit instantiation generates class Z<float>
return 0 ;
}
  1. Consider the following sample. This sample uses the template class members Z<T>::f() and Z<T>::g().
template <class T>
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z<int> zi ; //implicit instantiation generates class Z<int>
zi.f() ;    //and generates function Z<int>::f()
Z<float> zf ; //implicit instantiation generates class Z<float>
zf.g() ;      //and generates function Z<float>::g()
return 0 ;
}

This time in addition to the generating classes Z<int> and Z<float>, with constructors and destructors, the compiler also generates definitions for Z<int>::f() and Z<float>::g(). The compiler does not generate definitions for functions, nonvirtual member functions, class or member class that does not require instantiation. In this example, the compiler did not generate any definitions for Z<int>::g() and Z<float>::f(), since they were not required.


  1. Consider the following sample. This is an example of explicit instantiation of a class template.
template <class T>
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
template class Z<int> ; //explicit instantiation of class Z<int>
template class Z<float> ; //explicit instantiation of 
                          //class Z<float>
return 0 ;
}
  1. Consider the following sample. Will the compiler generate any classes in this case? The answer is NO.
template <class T>
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z<int>* p_zi ; //instantiation of class Z<int> not required
Z<float>* p_zf ; //instantiation of class Z<float> not required
return 0 ;
}

This time the compiler does not generate any definitions! There is no need for any definitions. It is similar to declaring a pointer to an undefined class or struct.

  1. Consider the following sample. This is an example of implicit instantiation of a function template.
//max returns the maximum of the two elements
template <class T>
T max(T a, T b)
{
    return a > b ? a : b ;
}
void main()
{
int I ;
I = max(10, 15) ; //implicit instantiation of max(int, int)
char c ;
c = max('k', 's') ; //implicit instantiation of max(char, char)
}

In this case the compiler generates functions max(int, int) and max(char, char). The compiler generates definitions using the template function max.

  1. Consider the following sample. This is an example of explicit instantiation of a function template.
template <class T>
void Test(T r_t)
{
}

int main()
{
//explicit instantiation of Test(int)
    template void Test<int>(int) ;
    return 0 ;
}


NOTE: Visual C++ 5.0 does not support this syntax currently. The above sample causes compiler error C1001.

In this case the compiler would generate function Test(int). The compiler generates the definition using the template function Test.

  1. If an instantiation of a class template is required, and the template declared but not defined, the program is ill-formed. VC5.0 compiler generates error C2079.
template <class T> class X ;

int main()
{
    X<int> xi ; //error C2079: 'xi' uses undefined class 'X<int>'
    return 0 ;
}
  1. Instantiating virtual member functions of a class template that does not require instantiation is implementation-defined. For example, in the following sample, virtual function X<T>::Test() is not required, VC5.0 generates a definition for X<T>::Test.
template <class T> 
class X 
{
    public:
        virtual void Test() {} 
};

int main()
{
    X<int> xi ; //implicit instantiation of X<int>
    return 0 ;
}

In this case the compiler generates a definition for X<int>::Test, even if it is not required.


Advantages of C++ template:

C++ templates enable you to define a family of functions or classes that can operate on different types of information. Use C++ templates in situations that result in duplication of the same code for multiple types.

Conclusion of  function C++ Template:

In functions overloading, more than one function with the same name is declared and defined. This makes the program more complex and lengthy. To overcome this problem, the function template is used.

In a function template, a single type less function is defined such that arguments of any standard data type can be passed to the function.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button