C++ Template Function & how to use template in c++ programming

C++ Template Introduction:

C++ Template-this is a very detail tutorial about c++ templates in this tutorial you will learn how to use the template in c++ programming.so let’s get started!

What is the C++  template in programming:

C++ templates 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.

The general syntax of c++ template:

template <class T>

T f-name (T argument (s))





keyword template is used to define the function template.


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.


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.


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:

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


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:

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:

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:

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.

  1. Consider the following sample. This sample uses the template class members Z<T>::f() and Z<T>::g().

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.

  1. Consider the following sample. Will the compiler generate any classes in this case? The answer is NO.

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.

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.

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.

  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.

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.

Related Articles

Leave a Reply

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

Check Also
Back to top button