Template aliases in C++ provide a way to create an alias (or shorthand) for a templated type, making the code more readable and easier to manage, especially when dealing with complex types. This feature was introduced in C++11 and allows you to define a type alias using the using keyword. Template aliases are particularly useful when you have long or complex type names that you want to simplify for repeated use.
The basic syntax for creating a template alias is:
template<typename T>
using AliasName = ExistingTemplate<T>;
template<typename T>
: Defines the template parameter(s).AliasName = ExistingTemplate<T>;
: Creates an alias AliasName for the template ExistingTemplate<T>
.Let’s say you have a complex template like std::map<T, std::vector<T>>
. You can create an alias to simplify the usage of this type:
#include <iostream>
#include <map>
#include <vector>
template<typename T>
using MapOfVectors = std::map<T, std::vector<T>>;
int main() {
MapOfVectors<int> myMap;
myMap[1].push_back(10);
myMap[1].push_back(20);
for (const auto& item : myMap[1]) {
std::cout << item << " ";
}
return 0;
}
template<typename T>
using MapOfVectors = std::map<T, std::vector<T>>;
creates a template alias MapOfVectors
for the type `std::map, you can simply use
MapOfVectorsSuppose you have a function pointer type that you use frequently. Instead of repeatedly writing out the full type, you can create a template alias for it.
#include <iostream>
// A function pointer type: int(*)(T, T)
template<typename T>
using Comparator = int(*)(T, T);
int compareInts(int a, int b) {
return a - b;
}
int main() {
Comparator<int> comp = &compareInts;
std::cout << "Comparison result: " << comp(10, 20) << std::endl;
return 0;
}
template<typename T>
using Comparator = int(*)(T, T);
creates an alias Comparator for a function pointer type that takes two arguments of type T
and returns an int
.Comparator<int>
as a shorthand for int(*)(int, int)
.Consider a scenario where you are dealing with nested template types like std::vector<std::pair<T1, T2>>
. A template alias can simplify this:
#include <iostream>
#include <vector>
#include <utility>
template<typename T1, typename T2>
using PairVector = std::vector<std::pair<T1, T2>>;
int main() {
PairVector<int, std::string> vec;
vec.push_back({1, "One"});
vec.push_back({2, "Two"});
for (const auto& item : vec) {
std::cout << item.first << " : " << item.second << std::endl;
}
return 0;
}
template<typename T1, typename T2>
using PairVector = std::vector<std::pair<T1, T2>>;
creates a template alias PairVector
for the nested template type `std::vectorPairVector<int, std::string>
instead of writing std::vector<std::pair<int, std::string>>
.Template aliases can also be used with templates that have default parameters. This can be particularly useful when you want to provide an alias that includes a commonly used default parameter.
#include <iostream>
#include <map>
template<typename T, typename Compare = std::less<T>>
using SortedMap = std::map<T, int, Compare>;
int main() {
SortedMap<int> myMap; // std::map<int, int, std::less<int>> by default
myMap[3] = 30;
myMap[1] = 10;
myMap[2] = 20;
for (const auto& item : myMap) {
std::cout << item.first << " : " << item.second << std::endl;
}
return 0;
}
template<typename T, typename Compare = std::less<T>>
using SortedMap = std::map<T, int, Compare>;
creates an alias SortedMap
for a std::map
with std::less<T>
as the default comparator.std::less
.Template aliases are a powerful feature in modern C++ that can significantly improve code clarity and maintainability, especially when working with complex template types.
Previous Page | Course Schedule | Course Content