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
Wrapper
template class takes a container type as a template parameter. - In the member function
getFirst
,typename T::value_type
specifies the type of the elements in the container. Thetypename
keyword is necessary becauseT::value_type
is a dependent type. - The function retrieves the first element of the container and returns it.
- In the
main
function, theWrapper
class 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
KeyValuePrinter
template class takes a map type as a template parameter. - The
using
statements use thetypename
keyword to define aliases for thekey_type
andmapped_type
of the map. - In the
print
function, these aliases are used to iterate over the map and print its key-value pairs. - In the
main
function, theKeyValuePrinter
class is instantiated with a map type, and the contents of the map are printed.
Key Points about typename
Keyword
- The
typename
keyword 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
typename
in required contexts results in a compilation error. - In modern C++,
typename
is often used in combination withusing
for creating type aliases.