Open In App

Where and Why Do I Have to Put the ‘template’ and ‘typename’ Keywords?

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, we have a template keyword to define template classes or functions and allow them to operate with generic types. On the other hand, we have typename keyword which is used to specify that the identifier that follows is a type, particularly in template code to clarify the ambiguity but confusion arises as to where exactly we need to put these keywords and why. So, in this article, we will discuss what is the use of template and typename keywords in C++.

Where to put template and typename Keywords?

The template keyword appears at the top of template declarations, acting as an indicator for the compiler to recognize the next code structure as a template.

For example, when we want to create a generic class that can handle any data type, we would start its definition with template<typename T>, where T is a placeholder for the data type that will be used.

template <typename T>
class Container {
    // Class implementation
};

The typename is often used in template definitions to indicate that a particular identifier is a type rather than a value.

For example, it is commonly seen in template parameters (e.g., template<typename T>) and also when you need to refer to a type that is dependent on a template parameter (e.g., typename T::subType where T is a template parameter and subType is a type defined within T).

template <typename ContainerType>
void printFirstElement(const ContainerType& container) {
    // implementation
}

Example

The below example demonstrates the declaration and use of template and typename keywords.

C++




// C++ program to demonstrate the declaration and use of
// template keyword.
  
#include <iostream>
using namespace std;
  
template <typename T> T add(T a, T b) { return a + b; }
  
int main()
{
    int sum_int = add(5, 10);
    double sum_double = add(3.5, 7.2);
    cout << "Sum of two int type values: " << sum_int
         << endl;
    cout << "Sum of two double type values: " << sum_double
         << endl;
    return 0;
}


Output

Sum of two int type values: 15
Sum of two double type values: 10.7



Why Use ‘template’ and ‘typename’ Keywords?

  • template: This keyword is essential for the creation of generic programming in C++, allowing for code reusability and type safety without the cost of performance. Adding templateat the beginning makes the code clearer and easier to read. It signals to developers that the code is flexible and can handle different data types, improving clarity for both developers and the compiler.
  • typename: It helps to resolve ambiguity in template code, ensuring that the compiler correctly interprets the code as intended by the programmer, especially in complex template uses where the distinction between types and non-types can become blurred.

Conclusion

In conclusion, using the ‘template’ keyword at the beginning of declarations tells the compiler that the code uses template programming, allowing for adaptability with different data types. On the other hand, using ‘typename’ inside the template body avoids ambiguity in dependent names, reducing potential compilation issues and placing the code’s overall stability and readability.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads