Home Resource Centre C++ Templates | Types, Usage, Overloading & More (+Code Examples)

C++ Templates | Types, Usage, Overloading & More (+Code Examples)

The C++ templates are a feature of the C++ programming language that makes it possible to carry out generic programming or write generic code. Generic programming is a paradigm for writing reusable, adaptable code that can operate on various data types and heavily relies on templates.

In other words, templates serve as the building blocks for generating the functions or classes rather than making separate functions or classes for each distinct data type. In this article, we will discuss everything related to C++ templates, how they work, how to use them, and more.

What Are Templates In C++ & How Do They Work?

In C++, templates are defined using the template keyword and type parameters, which are substituted with actual data types at compile time.

  • C++ templates use placeholders referred to as template parameters to represent different data types, which can be user-defined and built-in types.
  • When using a template, the compiler automatically creates the correct version of the function or class for the specified data type.
  • A procedure known as template instantiation takes place at compile time, which leads to creating a new function with the specific datatype.

This generates specialized code for each data type, providing flexibility, code reusability, and efficient execution for various scenarios. Then, we'll discuss how templates work in C++ programming.

Working Mechanism Of C++ Templates

  1. Template Definition: As mentioned before, templates are defined using the template keyword, followed by the template parameter(s) enclosed in angle brackets (<>). Template parameters can represent data types (typename T) or non-type parameters (int N), like array size.
  2. Template Instantiation: When a function or class template is used with a specific data type (e.g., add<int>(2, 3) or MyVector<double>), the compiler generates the appropriate version of the function or class for that data type. This process is called template instantiation.
  3. Code Generation: During template instantiation, the compiler generates a unique version of the function or class for each data type used. This avoids the runtime overhead and ensures efficient code execution.
  4. Type Deduction: In many cases, C++ can deduce the template parameter types automatically based on the arguments provided when calling a function template. This is called template argument deduction.
  5. Code Reusability: C++ Templates promote code reusability by allowing you to write a single generic function or class that can be used with different data types, reducing the need for redundant code.
  6. Specialization: You can provide specialized versions of function or class templates for specific data types, enabling custom behavior for those cases.

Let us take a look at a C++ template example to better understand the topic. 

Code Example:

Output:

20
20.6

Explanation:

In the simple C++ program above, we first include the <bits/stdc++.h> header, which is a convenience header provided by some C++ compilers to include commonly used standard headers. It includes most of the standard C++ library headers in one go.

  1. We then use the namespace std statement, which helps bring all the standard library symbols (e.g., cout, endl) into the current scope. It allows you to use these symbols without explicitly specifying the std:: prefix.
  2. (Function Template Declaration) Then, we declare a function template using the template keyword, and the template typename is T, which serves as the placeholder for the data type of the parameters and return value.
  3. (Function Definition) Next, we define the function template and name the function Max() using the type name T. 
  4. The function takes two parameters, a and b, of the data type T and returns a value of type T. It first compares the values of a and b using the greater than operator and then returns the larger value. We use the ternary operator (?:) in the expression.
  5. In the main() function, we call the Max() function with integers 10 and 20 as arguments inside a cout statement.
  6. Since the template parameter T is deduced to be integer type (int), the function returns the larger integer (20) and prints it.
  7. We, once again, call the Max() function passing values 10.5 and 20.6 (of type double) as arguments. Here, the template parameter T is deduced to be double, and the function returns the larger double (20.6), which is printed using cout.

Note: C++ deduces the actual data types for the function template based on the argument types provided during the function calls. This is known as template type deduction, which allows the Max function to work with both integers and floating-point numbers without the need for explicit type specification.

Types Of Templates In C++

There are two primary types of templates in C++, i.e., function templates and class templates. We will discuss both of these in detail in the sections ahead, but first, here is a brief description of both: 

  • Function Templates: Function templates allow you to define a single function that can work with multiple data types. They are defined using the template keyword, followed by template parameters inside angular/ angle brackets <>. These parameters represent the generic types of data on which the function can operate.
  • Class Templates: Class templates allow you to create generic classes with various data types. Like function templates, they are defined using the template keyword and template parameters inside angular brackets <>.

Besides the two types mentioned above, there are some other types of C++ templates that aren't commonly used. They are as follows: 

  • Alias Template (since C++11): An alias template provides a new name (alias) for a template instantiation. It simplifies complex type names and enhances code readability by creating a shorter, more descriptive alias for a specific template instantiation.
  • Variable Template (since C++14): A variable template is a blueprint for creating generic variables that can be parameterized with types or non-type values. It allows you to define families of variables that share common properties but can have different types or values.
  • Concept (Constraints and Concepts, since C++20): A concept is a set of requirements that define a set of valid template arguments. It enables template constraints, allowing you to specify which types or expressions are valid for a template, resulting in more readable error messages and better template error handling.

What Are Function Templates In C++?

As the name suggests, C++ template functions are those templates that help us create multiple functions of the type defined in the template. This way, a single function can operate on several data kinds, eliminating the need to write separate code for each type. Generic programming extensively uses function templates to improve the reuse and maintainability of code.

Why Use Function Templates In C++ Programs?

Function templates have many uses, such as making standardized functions in generic programming and many more. Some more benefits are :

  • They help avoid code duplication.
  • Code becomes cleaner and more condensed with the use of the C++ template functions.
  • They increase the flexibility and ease of maintenance of programs.

Syntax Of Function Template In C++

template <typename T>
return_type functionName(T parameter) {
// Function code here
// You can use the 'parameter' of type T in the code
// Return value of type 'return_type'
}

Here,

  • The template keyword indicates that there is a template declaration, in this case, a function template.
  • <typename T> is a template parameter declaration, where T represents the placeholder for the data type. You can use any valid identifier instead of T.
  • return_type refers to the data type of the function template's return value.
  • The functionName refers to the name of the function template.
  • T parameter reflects the function template's parameter, which is of type T.

Defining A Function Template In C++

Function templates use template parameters as placeholders for the data types of the function's arguments and return type. These template parameters are defined using the template keyword, followed by the typename keyword (or class keyword, which is equivalent in this context) and a placeholder name, typically represented by T.

For example:

template <typename T>
T getMax(T a, T b) {
return (a > b ? a : b);}

In this example, T is the template argument which can be any of the different data types (e.g., int, float). The typename keyword is used to indicate that T is a type parameter.

How To Call A Function Template?

To call a function template in C++ (or function template instantiation), you provide specific data types or values as arguments when calling the function. This process is similar to calling a regular function. However, with function templates, the C++ compiler automatically deduces the template arguments based on the function arguments you provide.

This is how a function template serves as a blueprint to generate multiple versions of the function based on the provided types or values. Look at the snippet showing how to call a function template in the main function, given below.

int main() {
int x = 5, y = 10;
int maxInt = getMax<int>(x, y); // Call the function template with int as the data type
float a = 3.14, b = 2.71;
float maxFloat = getMax<float>(a, b); // Call the function template with float as the data type
return 0;
}

In this example-

  • The expression getMax<int>(x, y) is the function call, which invokes the function template with int as the actual template parameter and variables x and y as the arguments.
  • The function call getMax<float>(a, b) invokes the function template with float as the data type and variables a and b as arguments. 

C++ Template Function Example

Now, let's look at a complete C++ program example showcasing how to use a C++ template function with different type parameters.

Code Example:

Output:

The sum of integers: 12
The sum of doubles: 5.85

Explanation:

In the C++ code example-

  1. We first define a function template AddNumbers, which takes two arguments of type T and returns their sum using addition arithmetic operator.
  2. Here, T is a placeholder for the data type that will be deduced by the compiler when the function is called.
  3. In the main() function, we use the AddNumbers function template with both int and double arguments.
  4. As mentioned in the code comments, we first call the AddNumbers() function with integer type and arguments 5 and 7. The outcome of this function is stored in the intSum variable.
  5. This creates and invokes the AddNumbers() function for integer type. It calculates the sum of the arguments and returns the same, which we print to the console using the cout statement.
  6. We again call the AddNumbers() function but with arguments 3.14 and 2.71, which are of type double.
  7. This invokes the AddNumbers() function for double type. It calculates the sum and the outcome is stored in the doubleSum variable.
  8. As is evident, the compiler automatically generates specialized versions of the function for int and double, allowing us to add numbers of different data types using the same function template.
  9. The output for both the function calls is printed to the console using the cout command.
  10. Lastly, the main() function terminates with a return 0 statement indicating successful execution.

The time complexity of the function template is O(1) as it performs a simple addition.
The space complexity is also O(1) as it uses a constant amount of memory to store the arguments and return value, regardless of the data type used.

Restrictions Of Generic Functions

As you know, generic functions can operate on multiple data types without being limited to a specific type. They are implemented through C++ template functions and are designed to work with a variety of data types, allowing for code reusability and flexibility. However, there are a few shortcomings when using these functions. They are:

  1. Type Compatibility: Generic functions must work with a broad range of data types. As a result, they are subject to type compatibility constraints. Some operations or expressions might not be valid for all data types, leading to compilation errors. You need to ensure that the operations used in the generic function are supported by all possible data types.
  2. Overhead: Generic functions can introduce some overhead compared to specialized functions for specific data types. The compiler generates separate versions of the function for different data types, potentially increasing the binary size. However, this overhead is usually minimal compared to the benefits of code reusability.
  3. Performance considerations: The performance of generic functions can be a little sluggish in comparison to specialized functions. Also, the code produced by the compiler may not be as effective as code designed exclusively for one data type since it supports a variety of data types. This means there is performance disparity, though it is frequently insignificant.
  4. Code Bloat: Using generic functions extensively with many different data types might lead to code bloat. Each specialization increases the size of the executable, potentially impacting load times and memory usage. It is essential to strike a balance between code reusability and excessive code generation.
  5. Template Specialization Complexity: As generic functions become more complex, handling template specialization and managing code variations for specific data types can become challenging. Template specialization can sometimes be verbose and harder to maintain.

Despite these restrictions, generic functions remain a powerful tool whose judicious use can help you write more flexible and maintainable code while minimizing code duplication. To make the most of generic functions, it is essential to carefully consider the potential type compatibility issues and performance trade-offs.

C++ Template Functions With Multiple Parameters

Function templates can have multiple parameters of different data types, allowing you to create more flexible and versatile normal functions. They enable you to handle complex identical operations involving multiple data types while still benefiting from code reusability.

Syntax:

template <typename T1, typename T2, ...>
return_type FunctionName(T1 parameter1, T2 parameter2, ...) {
// Function code here
// You can use the 'parameter1', 'parameter2', ... of their respective types in the code
}

Here,

  • Like before, the template keyword indicates the declaration of a template.
  • <typename T1,typename T2,......> is the template parameter declaration, where T1,T2,... acting as placeholders for the multiple data type. You can have as many parameters separated by a comma.
  • The FunctionName and return_type refer to the name of the function and the data type of its return value, respectively. 

Code Example:

Output:

Result 1: 25
Result 2: 15.7

Explanation:

  1. In this example, we defined the function template Multiply using the keyword template, which takes two parameters of different data types, T1 and T2.
  2. The function mutlitplies the parameters and returns the product value, i.e., a*b.
  3. In the main() function, we declare and initialize two variables intNum (integer type) and doubleNum (double type) with values 5 and 3.14, respectively. 
  4. We then call the Multiply() function with intNum as both the arguments. The outcome is stored in the result1 variable with type auto. 
  5. Then, we call the Mulitply() function again, but now with doubleNum as both the arguments and the outcome is stored in resutl2 (type auto).
  6. The function template generates a specialized version of the function, allowing us to multiply integers and mix different data types using the same template.
  7. In the call Multiply (intNum, intNum), both these arguments are of type int. So both T1, and T2 will be assigned integers as data types. 
  8. Similarly, in the call Multiply (intNum, doubleNum), the template T1 will be assigned integer type, and T2 will be assigned double as data type. 
  9. We print both the results to the console using the cout command

The time complexity of the function template for multiplication is O(1) because multiplication is a constant-time operation for built-in numeric data types like int and double.
The space complexity of the function template is also O(1) since it uses a constant amount of memory regardless of the data types involved.

C++ Template Function Overloading

Function templates in C++ can also be overloaded, just like regular functions. Overloading a function template allows you to define multiple versions of the template, each tailored to handle specific cases or data types. This enables you to provide specialized implementations for different scenarios while still benefiting from the generic nature of the C++ template.

Syntax:

// Function template
template <typename T>
return_type FunctionName(T parameter) {
// Generic implementation
}

// Overloaded function template
template <>
return_type FunctionName<>(T parameter1, T parameter2) {
// Specialized implementation for the given data types
}

Here, the first section of the syntax is the same as the function template declaration.

  • For the overloading function template, the keyword template is followed by empty angular brackets <> since it uses the same template parameter as the original function. 
  • T parameter1 and T parameter2 refer to the specialized data types.
  • The curly brackets {} contain specialized implementations for the specific data types within the function body.

Code Example:

Output:

Max integer: 9
Max double: 3.14
Max string: Banana

Explanation:

In the example C++ code-

  1. We first define a function template Max using the template keyword and the template parameter typename T. This template allows the function to work with various data types.
  2. The function template compares two values, a and b, and returns the larger value of the two.
  3. Then, we overload the template function by defining the Max() function with constant character pointer or C-style string as data type (template parameter), i.e., const char*
  4. This version of the function allows the code to handle C-style strings differently than other data types. It also compares the function parameters and returns the larger of two.
    • Note that the C-style strings (character pointers) need special handling because the standard comparison operator (>) cannot be directly used with them.
    • To handle this the specialized version uses the strcmp() function to compare the strings and returns the larger string based on lexicographical order.
  5. Inside the main() function, we use the MAX template function three times. The first two calls reflect the generic use of the template.
  6. The first call invokes MAX to find the maximum of two integers and the second the maximum of two double type values. The template parameter T is deduced as int and double, respectively.
  7. Next, we declare two character pointers, str1 and str2, and initialize them with string values Apple and Banana, respectively. 
  8. In the third call to MAX, we pass str1 and str2 as arguments. This invokes the specialized version of MAX. 
  9. The result of all the functions is printed to the console using the cout command

The time complexity of the function template is O(1) for comparisons and O(N) for C-style string comparison (using strcmp).
The space complexity is also O(1) as it uses a constant amount of memory to store the arguments and return value, regardless of the data types used.

What Are Class Templates In C++?

Class Templates are another way of using templates to reduce the need for code duplication. They help us write generic classes and define a blueprint for a class that can work with different data types. Some of the most common reasons that we use the class templates in C++ programs are as follows:

  • When we want to design a generic implementation of a class.
  • Using class template specialization, we can write a single implementation of a class and then instantiate it with various data types.
  • This reduces code duplication and increases the flexibility and maintainability of code.

How Do C++ Templates Class Work?

The working mechanism of C++ template classes can be summarised in the following points:

  • The keyword template is used to define class templates, which are then followed by template parameters enclosed in angular brackets <>.
  • For the data types or non-type values that the class will operate on, these template parameters serve as placeholders.
  • When you create a new instance of the class, you specify particular data types for the template parameters, and the compiler creates a unique version of the class specifically for those types.

Syntax of Class Templates In C++

template <typename T>
class ClassName {
public:
// Member variables and functions that use types T
};

Here,

  • The template keyword indicates that we are declaring a template, and <typename T> refers to the template parameters where T is the placeholder. You can have as many template parameters as needed, each separated by a comma.
  • ClassName refers to the name of the class template. You can use any valid identifier as the class name.
  • The curly brackets {} contain the class template block consisting of the definition of member variables, class member functions, and other class elements that use the template parameters T1, T2, and so on.

Code Example:

Output:

The top element of intStack: 30
The top element of doubleStack: 2.71

Explanation:

In this example-

  1. We first define a class template called Stack to represent a stack data structure.
  2. The class template uses a vector (std::vector) from the C++ Standard Library to store its elements.
  3. The Stack class template consists of 4 functions that make it a fully functional stack implementation. These are:
    • Push: Adds an element to the top of the stack using std::vector's push_back function.
    • Pop: Removes the top element from the stack using std::vector's pop_back function (if the stack is not empty).
    • Top: Returns the top element of the stack using std::vector's back function (if the stack is not empty). Otherwise, it throws a runtime_error.
    • IsEmpty: Uses an if-statement to check if the stack is empty with the std::vector's empty function.
  4. In the main() function, we create two instances of the Stack class, namely, intStack (for the int data type) and doubleStack (for the double data type).
  5. Next, we push three integers (10, 20, 30) onto the intStack, and then use the Top() function with the cout command to print the top element of the stack.
  6. Then, we push two double values (3.14, 2.71) onto the doubleStack and again display the top element to the console using Top() and cout. 

The time complexity is O(1) because most of the operations like push(), pop(), Top(), and Empty() are constant time operations.
The space complexity is considered as O(N) because the class template requires extra memory to store elements, and the vector may require additional memory for dynamic resizing.

Defining A Class Member Outside C++ Template Class

We have already discussed how to define C++ template classes and how to define members within the class. Wondering if we can define members outside the template?

Well, yes, we can. In fact, defining a class member outside the class template is similar to defining a regular member function of a non-template class.

  • For this, we must declare the member function inside the class template definition and then provide its implementation outside the class template using the scope resolution operator (::).
  • This technique is particularly useful for more complex member functions or when you want to hide the implementation details from the header file.

Let's extend the previous Stack class template example and see how to define a member function outside the class template.

Code Example:

Output:

Size of the stack: 3

Explanation:

Just like in the previous example, we define a template class Stack with 4 functions- Push (pushes the argument into the vector), Pop (removes the last element of the stack), Top (returns the top element of the stack), IsEmpty (returns true if the stack is empty else return false).

  1. After the definition of four functions, we declare a new member function PrintSize() to the Stack class template.
  2. The implementation of the PrintSize function is defined outside the class template using the template keyword followed by <typename T> and the scope resolution operator (::).
  3. In the main() function, we created an instance of the Stack class template called intStack and added three elements to it using the Push() function.
  4. Next, call the PrintSize() member function, which prints the size of the stack to the console using the cout command.

Time and Space Complexity: The time complexity of the PrintSize() function is O(1), and space complexity is the same as explained previously, i.e., O(N).

C++ Template Class With Multiple Parameters

Just like we discussed in the case of the C++ template function, the C++ template classes can also have multiple template parameters. Class templates with multiple parameters allow us to create versatile and flexible generic classes that can work with multiple data types simultaneously.

How To Create C++ Template Class With Multiple Parameters?

When defining a class template with multiple parameters, we provide a list of template parameters within angular brackets <>. The syntax for this is mostly the same as that of the C++ template class declaration. The only difference is that the angular brackets will contain more than one parameter type.

Syntax:

template <typename T1, typename T2, ...>
class ClassName {
public:
// Member variables and functions that use types T1, T2, ...
};

Here, each template (i.e., typename T1, typename T2, etc.) parameter represents a different data type. When we create an instance of the class, we specify specific data types or values for each parameter, and the compiler generates a specialized version of the class accordingly. Now, let's take a look at an example for better understanding.

Code Example:

Output:

First: 10, Second: 3.14
First: hello, Second: 1

Explanation:

In the sample C++ code-

  1. We define a class template called Pair, which represents a pair of elements with two different types.
  2. The class template has two private member variables, first and second, of types T1 and T2, respectively.
  3. Next, we have a public constructor, which takes two parameters, a of type T1 and b of type T2. The constructor, initializes member variables/data members first and second with values a and b, respectively.
  4. Then, we define two public member functions in the class, called  GetFirst() and GetSecond(). These functions allow external access to the values of the private member variables first and second, respectively.
  5. In the main() function, we create two objects of the Pair class using different template arguments.
    • The first object pair1 takes int and double template arguments, which are initialized with the values 10 and 3.14, respectively.
    • The second object, pair2, takes string and bool template arguments which are initialized with the values hello and true, respectively.
  6. Then, we call the GetFirst() and GetSecond() member functions of both objects to retrieve and print the values of their member variables using cout.
  7. This demonstrates how to access the private member variables of the Pair class using the public member functions.

The time complexity of operations on class templates with multiple parameters depends on the operations performed inside the class. In this example, the time complexity of the constructor, GetFirst, and GetSecond member functions is O(1) as they involve simple assignments and returns.

The space complexity of the Pair class template is O(1) as it uses a constant amount of memory to store the first and second member variables, regardless of the data types used. The memory consumption is constant and does not depend on the number of elements stored.

What Is C++ Template Specialization?

Templates specialization is a powerful feature in C++ that allows you to write custom implementations for specific data types when using function templates or class templates. It enables you to override the default behavior of a template for certain types and tailor the code to handle them differently.

Template class specialization is particularly useful when the default template implementation is not suitable for certain data types, and you need a specific solution.

Template Parameter & C++ Template Specialization

The term template parameter simply refers to the type of parameter to be passed into the template for the specific implementation. It is a placeholder for the type or non-type values in a template declaration.

This eliminates the need to write multiple implementations of the same function or classes with only changes in their datatypes by allowing us to write generic as well as specific implementations. There are two types of template parameters:

1. Type Parameters:

These are template parameters that represent data types. They allow you to define classes or functions that can work with various data types. For example, template <typename T> or template <class T>. These are declared using keywords typename and class followed by an identifier which serves as the template parameter name.

Syntax:

template <typename T>
OR
template <class T>

Code Example:

Output:

15
5.85

Explanation:

  1. In this example, we define a function template where the template parameter T represents a generic data type.
  2. The add function can be called with different data types (e.g., int, double) while using the same code for addition. The compiler generates specialized versions of the add function based on the provided data types.
  3. In the first call, inside the main() function, the compiler generates a version of add for int, and in the second call, it generates a version for double.
  4. This allows for efficient code execution with different data types, all using the same function template.

2. Non-type parameter:

A non-type template parameter in C++ allows you to pass constant values as template arguments instead of data types. This means you can create functions or classes that are parameterized by a specific constant value, allowing the compiler to generate specialized versions of the function or class for different constant values at compile time.

Syntax:

template <data type N> //here datatype is what we want e.g. int

Code Example:

Output:

50
21
-30

Explanation:

  1. The template parameter <int N> denotes that the function template accepts a parameter of type integer constant, denoted by N.
  2. The template function called MultiplyByConstant takes the parameter x and multiplies it by constant N to return the outcome.
  3. In the main() function, we create three instances of the MultiplyByConstant function template with the constant values 5, 3, and -2, respectively. The outcomes of these functions are stored in variables result1, result2, and result3, respectively.
  4. The template parameter <5> indicates that the value of N will be changed to 5, so the result for MultiplyByConstant<5>(10) will be 50 (i.e., 10 * 5).
  5. Similarly, for the template parameters <3> and <-2>, the results will be 21 (i.e., 7 * 3) and -30 (i.e., 15 * -2), respectively.
  6. All the results are printed to the console using the cout command

Working Mechanism Of C++ Template Specialisation

When we use generic templates in our code and call them using different types of parameters, the compiler automatically copies the called function with the parameter type that we used to call it. This copy is called a specific version of the template.

But internally, the compiler searches for the closest match, i.e., a function with the same type of parameters used in the function call, i.e., the specialized template is searched first, and if it is available, it is invoked and used.

Code Example:

Output:

25
9.8596

Explanation:

  1. In this example, we first create a template function, Square, which takes an input and returns its square value.
  2. We also provide a template specialization for this function with the double data type to handle the square of a floating-point number differently than the generic template.
  3. Then, in the main() function, we call the Square(5) function. The compiler uses the generic template and generates 5 * 5 = 25.
  4. We again call the Square function, with double type value 3.14. In this case, the compiler detects the specialized version for double and generates 3.14 * 3.14 = 9.8596.
  5. The results are hence produced accordingly.

Can We Pass Non-type Parameters To C++ Templates?

Yes, you can pass non-type parameters to templates in C++ programming. Non-type template parameters are constant values that are used to customize the behavior of template classes or functions.

  • These parameters can be integral, enumeration, or pointer types (starting from C++11) and are known at compile-time.
  • By using non-type parameters, you can create specialized versions of templates based on specific constant values.

For example, you can define a fixed-size array using a non-type parameter to determine the size:

template <int Size>
class FixedSizeArray {
// Class implementation with a fixed size of Size
};
FixedSizeArray<10> array1; // Instantiate with size 10
FixedSizeArray<20> array2; // Instantiate with size 20

You can also use non-type parameters to create template functions that operate differently depending on the provided constant value, offering a high degree of customization while maintaining efficiency and type safety.

Note- The non-type template parameters must be constants known at compile-time and have certain limitations depending on their type.

How To Specify Default Arguments For Templates In C++?

In C++, we can easily define the default arguments for functions as well as class templates. This allows us to give default values or data types when no value or datatype is passed into the function call.

To specify default arguments for templates in C++, we can use the assignment operator (=) after the template parameter declaration, followed by the default value.

Code Example:

Output:

Result1: 5
Result2: 30
Result3: 3.14

Explanation:

In this example, we have a function template named Add with a default template argument for the data type (typename T = int). This means if no explicit data type is provided during the function call, the function will use int as the default data type.

Let's break the code down further: 

  1. The Add function template takes two parameters of type T, where the second parameter, b, has a default value of 0. This means that if not provided explicitly during the function call, the default value 0 will be used.
  2. The function calculates the sum of two values of the same data type, T, and returns the result.
  3. In the main() function, we first call the Add function with only one argument, i.e., Add(5). 
  4. Since the value for b isn't provided, the default value 0 is used. Therefore, the result of Add(5) will be 5 + 0, which is stored in variable result1.
  5. The second call to Add is made with the value of both parameters provided explicitly. So, Add(10, 20) will calculate the sum as 10 + 20 and store the outcome in variable result2. 
  6. Next, we call Add for a third time with a double data type parameter. Here, we explicitly specify the data type as double by using <double> when calling the function.
  7. So, Add<double>(3.14) will calculate the sum as 3.14 + 0, with the default value for b, and the outcome is stored in variable result3.
  8. Finally, the cout statements are used to print the results of result1, result2, and result.

Advantages Of C++ Templates 

There are many advantages of using C++ templates in our code. Some of them are listed here:

  1. Code Reusability: Templates allow you to write a single piece of code that can work with multiple data types. This reduces code duplication and makes maintenance easier, as changes only need to be made in one place.
  2. Type Safety: The C++ templates guarantee type safety at compile time. That is, the compiler type checks the template argument to catch any potential type-related errors before the program runs.
  3. Performance Optimisation: The compiler can produce specialized versions of functions or classes using templates in C++ programs, which results in effective code execution. This is done at compile time, hence decreasing the runtime. Inlined functions, for instance, can lead to better performance as the compiler can tailor the generated code specifically for each type used.
  4. Flexibility: Templates offer a high degree of flexibility. You can use templates for functions, classes, and even template parameters, allowing you to tailor your code for various scenarios.
  5. Compile-time Polymorphism: Polymorphism simply implies multiple forms of the same function. We have seen that using templates, a specialized copy of the function is made at compile time; hence it is a way of compile time polymorphism.
  6. Generics: Templates provide a way to create generic functions and generic classes that can be used with different types. This is similar to how other programming languages use generics to achieve type flexibility.
  7. Standard Library: The C++ Standard Library heavily utilizes templates. Many container classes (like std::vector, std::map, etc.) are implemented as templates. This allows these containers to work with various data types seamlessly.
  8. Custom Data Types: When you create your own data structures or classes, templates can help you define functions or operators that work consistently with your custom types.
  9. Function Overloading: Templates can provide an alternative to function overloading when you need to perform similar operations on different data types. Instead of writing multiple overloaded functions, you can use a template function to cover all cases.
  10. Reduced Maintenance: Without templates, if you were to change an algorithm or function, you'd need to update the code for each specific data type. Templates reduce this maintenance burden by centralizing the common logic.
  11. Standardization: When designing libraries or APIs, templates can help you offer flexible and customizable components that users can use with their chosen data types.

Disadvantages Of C++ Templates

While there are many advantages to using templates in C++, there are also some disadvantages as well. Some of these are as follows:

  1. Compilation Time: Templates can significantly increase compilation times, especially for complex templates or when many different instantiations are required. Longer compilation times can slow the development process, particularly in large projects.
  2. Complex Error Messages: Compiler error messages related to templates can be notoriously complex and difficult to decipher. Debugging template-related errors may be challenging, especially for beginners.
  3. Abstraction Overhead: Overusing C++ templates can lead to excessive levels of abstraction, making the code harder to understand and maintain. It may become challenging for other developers to grasp the full behavior of the code.
  4. Verbose Syntax: Template syntax can be verbose and harder to read than regular C++ code. The code readability may also suffer, especially when dealing with complex template declarations.
  5. Increased Debugging Difficulty: Debugging templates can be more complicated than regular code. Understanding how templates are expanded and instantiated during compilation may require specialized knowledge.
  6. Steep Learning Curve: For newcomers to C++ programming, understanding and effectively using templates can be challenging. Learning the intricacies of template syntax and best practices may take time.

Despite these disadvantages, templates are an essential and powerful tool in C++ programming that increases code readability, maintenance, and flexibility.

Difference Between Function Overloading And Templates In C++

Both function overloading and templates are different things, and they have their separate use cases. Some of the differences between them are listed below:

Aspect

Function Overloading

Templates

Definition

Multiple functions with the same name but different parameter lists are defined.

A single function template is defined, which can work with multiple data types or values.

Purpose

It provides different implementations for the same operation based on different input parameters.

It leads to the creation of generic code that can work with various data types or values.

Parameter Types

Overloaded functions must have different parameter types or a different number of parameters.

Template parameters can be of any data type, and the template can work with multiple types or values.

Invocation

The appropriate function is selected at compile time based on the provided argument types.

The template is instantiated at compile time based on the specific data types or values used with it.

Syntax

Function names are the same, but the parameter lists differ.

A single template function is defined using template parameters.

Code Duplication

This may lead to code duplication if similar functionality is implemented in multiple functions.

It reduces code duplication as a single template can handle multiple data types or values.

Flexibility

Limited flexibility as new overloads need to be explicitly defined for each unique parameter combination.

Highly flexible as it allows code reuse and can work with various data types without additional definitions.

Code Reusability

Moderate reusability, as functions can be reused based on different argument types.

High reusability, as a single template, can be used with various data types and values.

Compile Time Error

Errors occur at compile time if an appropriate overload cannot be found for the given arguments.

Errors may occur during template instantiation if the template is not compatible with the provided data types or values.

Conclusion

In conclusion, we can say that C++ templates are a powerful tool for generic programming, enabling code flexibility and reusability. Function templates and class templates allow developers to write a single implementation that can work with various data types and values. They promote efficient code execution by generating specialized versions at compile time.

Templates enrich the C++ Standard Template Library (STL) with generic containers, algorithms, and data structures. However, they may lead to code bloat (longer codes) and longer compilation times. Despite these challenges, C++ templates are an essential aspect of modern programming, empowering developers to build efficient, type-safe, and versatile code for a wide range of applications.

Also read: 51 C++ Interview Questions For Freshers & Experienced (With Answers)

Frequently Asked Questions

Q. What is template vs. class in C++?

In C++, both templates and classes are fundamental features that serve different purposes. Let's take a closer look at the differences between templates and classes:

Class: A class in C++ is a blueprint for creating objects. The keyword class defines the structure and behavior of an object, encapsulating data members (attributes) and member functions (methods). Instances of a class are created as objects. Each object of a class has its own set of data members, but the code for member functions is shared among all instances of the class.

class MyClass {
public:
int data; // Data member
void display() {
// Member function
cout << "Data: " << data << endl;}
};

Template: A template in C++ is a way to define generic code that can work with multiple types. Templates allow you to create ordinary functions or classes that can be instantiated with different data types as needed. This enables you to write a single template definition that can be reused with different types, reducing code duplication and enhancing code flexibility.

template <typename T>
class MyTemplate {
public:
T data; // Data member of type T
void display() {
cout << "Data: " << data << endl;}
};

The differences between C++ templates and classes are as follows:

  1. Purpose:

    • Classes are used to define the blueprint for creating objects with specific attributes and behaviors.
    • Templates are used to create generic functions or classes that can work with multiple types.
  2. Type Flexibility:

    • Classes are designed for a specific type of object, and its members are fixed for that type.
    • Templates provide flexibility by allowing the same code to work with different types.
  3. Instantiation:

    • Classes are instantiated to create objects of a specific type.
    • Templates are instantiated with a specific data type when you use them, creating a specialized version of the template for that type.
  4. Code Duplication:

    • Classes can lead to code duplication if you want similar behavior for different types, requiring you to define multiple classes.
    • Templates help reduce code duplication by providing a single template definition that can be used with various types.
  5. Example:

    • A single class might define a specific type of container, like a linked list for integers.
    • A template might define a generic container, like a linked list that can hold various types of data.

In summary, classes are used to define object blueprints with fixed attributes and behaviors, while templates allow you to create flexible, generic code that can be instantiated with different types. The concept of a template is particularly useful for code reusability and type flexibility when you want to write functions or classes that work with multiple data types.

Q. What is the difference between template and inheritance in C++?

The difference between templates and inheritance in C++ lies in their fundamental purpose. That is, templates enable generic programming and code reusability across different data types, while inheritance facilitates the creation of class hierarchies and the implementation of the "is-a" relationship between classes.

Template

Inheritance

Used for generic programming, creating reusable code that works with multiple data types.

Used for creating class hierarchies and implementing the "is-a" relationship between classes.

Requires explicit instantiation with data types.

Implicitly applies to derived classes.

Works with functions and classes.

This only applies to classes and their members.

It provides a way to write generic algorithms and data structures.

Enables code sharing and polymorphism through base class pointers.

Template specialization allows customization for specific data types.

Inheritance allows customization through virtual functions and overridden methods.

Q. What is template <typename>?

The template <typename> is the syntax used to define templates in C++. The typename keyword indicates that the following identifier is a type and will be used as a placeholder for a specific data type when the template is instantiated with actual arguments.

Syntax:

template <typename T>
return_type FunctionName(T parameter) {
// Function code here
}

Here, 

  • T: This is the template type parameter. It represents a placeholder for an actual data type that will be determined at the time of template instantiation.
  • return_type: This is the return type of the function template.
  • FunctionName: This is the name of the function template.
  • T parameter: This is the function parameter of type T, representing the data of the specified type passed to the function template.

Q. What is the difference between a template and a macro in C++?

The main difference between templates and macros in C++ lies in their capabilities and how the compiler processes them.

Template

Macro

Type-safe: Templated code is type-checked by the compiler, ensuring type safety.

Not type-safe: Macros are simple text replacements and may lead to type-related issues.

Generates type-specific code: Templated code is generated for each data type used.

Textual substitution: Macros are text-based replacements before compilation.

Supports generic programming: Templates can work with multiple data types.

Limited to text substitution: Macros can only perform text replacements.

Encourages code reuse: Templates allow writing generic algorithms and data structures.

Prone to code bloat: Macros may generate duplicated code, increasing executable size.

Better debugging and error messages: Templated code errors are reported with clear error messages.

Difficult debugging: Macros may lead to obscure error messages and challenging debugging.

Q. Describe template vs. abstract class in C++.

The key difference between templates and abstract classes lies in their primary purpose and how they achieve code reusability. The differences are listed in the table below:

Template

Abstract Class

Used for generic programming with multiple data types.

Used for defining a common interface for a group of related classes.

Requires explicit instantiation with data types.

It cannot be instantiated directly as it serves as a base class for derived classes.

Template code is generated at compiler time for each data type used.

Abstract classes are resolved at runtime when creating objects of derived classes.

It provides a way to write generic algorithms and data structures.

Enables polymorphism and code sharing through virtual functions and inheritance.

C++ template classes can have member functions or variables specific to each data type.

Abstract classes can have pure virtual functions to enforce implementation in derived classes.

Q. What do templates do?

Templates in C++ enable generic programming, which is the ability to write code that can work with multiple data types or values without explicitly specifying those types at the time of writing the code. They serve as blueprints that allow you to create functions, families of classes, or even variables with placeholders for types or values. When we instantiate or use C++ templates with actual types or values, the compiler generates specialized versions of the code specific to those types or values.

By now, you must know what C++ templates are and how to use them. Here are a few other topics you must know:

  1. C++ Type Conversion & Type Casting Demystified (With Examples)
  2. What is Function Prototype In C++ (Definition, Purpose, Examples)
  3. Dynamic Memory Allocation In C++ Explained In Detail (With Examples)
  4. Static Member Function In C++: How to Use Them, Properties, & More
  5. OOPs Concept In C++ | A Detailed Guide With Codes & Explanations
Shivani Goyal
Manager, Content

An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.

TAGS
Engineering Computer Science C++ Programming Language
Updated On: 20 Jun'24, 04:00 PM IST