C++ typename Keyword
The typename keyword in C++ is used in templates to explicitly specify that a dependent name (a name that depends on a template parameter) represents a type. It is necessary to disambiguate cases where the compiler might interpret a name as something other than a type, such as a static member or variable.
The typename keyword is essential for ensuring that template code is correctly parsed and understood by the compiler. It is primarily used in contexts where dependent types appear in template classes or functions.
Syntax
</>
Copy
typename nested_type_name;
- typename
- The keyword used to specify that a dependent name is a type.
- nested_type_name
- The name of the dependent type that requires disambiguation.
When to Use typename
- When accessing a nested type within a template parameter.
- When defining an alias for a dependent type within a template.
- To resolve ambiguity when the compiler cannot determine whether a dependent name is a type or not.
Examples
Example 1: Accessing Nested Type
This example demonstrates how the typename keyword is used to access a nested type in a template parameter.
</>
Copy
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Wrapper {
public:
typename T::value_type getFirst(const T& container) {
return container[0];
}
};
int main() {
vector<int> numbers = {1, 2, 3};
Wrapper<vector<int>> wrapper;
cout << "First element: " << wrapper.getFirst(numbers) << endl;
return 0;
}
Output:
First element: 1
Explanation:
- The
Wrappertemplate class takes a container type as a template parameter. - In the member function
getFirst,typename T::value_typespecifies the type of the elements in the container. Thetypenamekeyword is necessary becauseT::value_typeis a dependent type. - The function retrieves the first element of the container and returns it.
- In the
mainfunction, theWrapperclass is instantiated withvector<int>, and the first element of the vector is printed.
Example 2: Defining an Alias for a Dependent Type
This example shows how to use the typename keyword to define an alias for a dependent type in a template.
</>
Copy
#include <iostream>
#include <map>
using namespace std;
template <typename T>
class KeyValuePrinter {
public:
using KeyType = typename T::key_type;
using ValueType = typename T::mapped_type;
void print(const T& container) {
for (const pair<KeyType, ValueType>& entry : container) {
cout << entry.first << ": " << entry.second << endl;
}
}
};
int main() {
map<string, int> ageMap = {{"Alice", 25}, {"Bob", 30}};
KeyValuePrinter<map<string, int>> printer;
printer.print(ageMap);
return 0;
}
Output:
Alice: 25
Bob: 30
Explanation:
- The
KeyValuePrintertemplate class takes a map type as a template parameter. - The
usingstatements use thetypenamekeyword to define aliases for thekey_typeandmapped_typeof the map. - In the
printfunction, these aliases are used to iterate over the map and print its key-value pairs. - In the
mainfunction, theKeyValuePrinterclass is instantiated with a map type, and the contents of the map are printed.
Key Points about typename Keyword
- The
typenamekeyword is required to specify that a dependent name represents a type. - It is commonly used in templates when accessing nested types in template parameters.
- Failing to use
typenamein required contexts results in a compilation error. - In modern C++,
typenameis often used in combination withusingfor creating type aliases.
