How to Use typename Keyword in C++?
Last Updated :
11 Jun, 2024
In C++, the "typename" is a keyword that was introduced to declare a type. It is used for specifying that the identifier that follows is a type rather than a static member variable. In this article, we will learn how to use the typename keyword in C++.
C++ typename Keyword
The typename keyword is mainly used for two cases:
- Declaring Template Type Parameters
- In the Type Declaration
1. Declaring Template Type Parameters
cppCopy codetemplate <typename T>
class MyClass {
// Class definition
};
In this context, typename is used to declare a template parameter T as a type.
2. In the Type Declaration
Inside member functions of class templates, typename is used when referring to nested types from template parameters.
cppCopy codetemplate <typename T>
class MyClass {
public:
void myMethod() {
typename T::nested_type variable;
// 'typename' is necessary here to specify that T::nested_type is a type
}
};
Here, T is a template parameter.
C++ Program for Demonstrating the Usage of typename Keyword
The below program demonstrates how to use a typename keyword in C++.
C++
// C++ Program to use the typename keyword
#include <iostream>
#include <vector>
using namespace std;
// function for printing elements of a container
// passed as parameter
template <typename T> void printElements(const T& container)
{
// loop using the iterator it of container type
for (typename T::const_iterator it = container.begin();
it != container.end(); ++it) {
// Dereferencing iterator for getting the element
// and print it
cout << *it << " ";
}
// Print a newline character after all elements are
// printed
cout << endl;
}
int main()
{
// Create a vector vec and initialize it
vector<int> vec = { 1, 2, 3, 4, 5 };
// Use the template function to print elements of the
// vector
cout << "Elements: ";
printElements(vec);
return 0;
}
OutputElements: 1 2 3 4 5
Time Complexity: O(1), considering vector has constant number of elements.
Auxilliary Space: O(1)
Explanation: In the above example typename keyword is used to indicate that T::const_iterator is a type. If we do not use typename here then the compiler will throw an error because here T is dependent on template parameter and to specify that it is not a static member variable to the compiler we have to use typename keyword here.
Similar Reads
C++ Program to Show Use of This Keyword in Class Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. There are 4 wa
4 min read
How to Find the Type of an Object in C++? In C++, every variable and object has a type. The type of an object determines the set of values it can have and what operations can be performed on it. Itâs often useful to be able to determine the type of an object at runtime, especially when dealing with complex codebases. In this article, we wil
2 min read
Where and Why Do I Have to Put the 'template' and 'typename' Keywords? 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 confusi
3 min read
How to Create a Stack of User-Defined Data Type in C++? In C++, we have a stack data structure that follows the LIFO (Last In First Out) rule and allows operations such as push, pop, and top at one end of the structure. In this article, we will learn how to create a stack of user-defined data types in C++. Example: Input://user defined data typestruct Po
2 min read
How to Resolve a Name Conflict in C++? In C++, naming conflict occurs when two identifiers in the same scope have the same name and the compiler cannot infer which identifier to refer to when it is mentioned in the program. In this article, we will discuss what are name conflicts, what are its causes, and how to resolve the name conflict
4 min read