Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Template aliases and variadic templates | Template Metaprogramming
C++ Templates
course content

Course Content

C++ Templates

C++ Templates

1. Creating First Template
2. Templates Usage
3. Template Specialization
4. Class
5. Template Metaprogramming

Template aliases and variadic templates

Template Aliases

Template aliases, introduced in C++11, enable the creation of aliases for complex template types. They improve code readability by providing descriptive names for template instantiations.

  • template : This line declares a template parameter T that can be used within the alias declaration.
  • using MyAlias = SomeComplexTemplate<T>;: This line creates an alias MyAlias for the template SomeComplexTemplate instantiated with type T.

Let's consider a scenario where we have a complex template type, which may involve a lot of template parameters and nested types. Instead of repeatedly typing out the complex type, we can create a template alias for it:

cpp

main

copy
123456789101112
template <typename T> struct MyContainer { // Implementation details }; // Alias for MyContainer<int> using IntContainer = MyContainer<int>; int main() { IntContainer container; // Use IntContainer... }

Everything was clear?

Section 5. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt