template_alias


Concept: template alias

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.

Syntax of Template Aliases

The basic syntax for creating a template alias is:

template<typename T>
using AliasName = ExistingTemplate<T>;

Example 1: Basic Template Alias

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;
}

Explanation:

Example 2: Alias for a Function Pointer Type

Suppose 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;
}

Explanation:

Example 3: Simplifying Nested Templates

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;
}

Explanation:

Example 4: Alias for Template Classes with Default Parameters

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;
}

Explanation:

Summary

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