Template is simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write same code for different data types. For 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.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second keyword can always be replaced by keyword ‘class’.
How templates work?
Templates are expanded at compiler 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.
Function Templates We write a generic function that can be used for different data types. Examples of function templates are sort(), max(), min(), printArray()
7 7 g
Below is the program to implement Bubble Sort using templates in C++:
Sorted array : 10 20 30 40 50
Class Templates Like function templates, class templates are useful when a class defines something that is independent of data type. Can be useful for classes like LinkedList, BinaryTre, Stack, Queue, Array, etc.
Following is a simple example of template Array class.
1 2 3 4 5
Can there be more than one arguments to templates?
Yes, like normal parameters, we can pass more than one data types as arguments to templates. The following example demonstrates the same.
Constructor Called Constructor Called
Can we specify default value for template arguments?
Yes, like normal parameters, we can specify default arguments to templates. The following example demonstrates the same.
What is the difference between function overloading and templates?
Both function overloading and templates are examples of polymorphism feature of OOP. Function overloading is used when multiple functions do similar operations, templates are used when multiple functions do identical operations.
What happens when there is static member in a template class/function?
Each instance of a template contains its own static variable. See Templates and Static variables for more details.
What is template specialization?
Template specialization allows us to have different code for a particular data type. See Template Specialization for more details.
Can we pass nontype parameters to templates?
We can pass non-type arguments to templates. Non-type parameters are mainly used for specifying max or min values or any other constant value for a particular instance of template. The important thing to note about non-type parameters is, they must be const. Compiler must know the value of non-type parameters at compile time. Because compiler needs to create functions/classes for a specified non-type value at compile time. In below program, if we replace 10000 or 25 with a variable, we get compiler error. Please see this.
Below is a C++ program.
What is template metaprogramming?
See Template Metaprogramming
You may also like to take a quiz on templates.
Java also support these features. Java calls it generics .
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Templates and Default Arguments
- Variadic function templates in C++
- Templates in C++ vs Generics in Java
- Templates and Static variables in C++
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Passing and Returning Objects in C++
- Train a Support Vector Machine to recognize facial features in C++
- unordered_set operators in C++ STL
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- How to delete a range of values from the List using Iterator
- How to create a List with Constructor in C++ STL
- How to delete last element from a List in C++ STL
- Can C++ reference member be declared without being initialized with declaration?
- Finding Floor and Ceil of a Sorted Array using C++ STL
Improved By : anu7699