Today, I write about two topics: alias templates and template parameters. Alias templates are a way to give a name to a family of types. Template parameters can be types, non-types, and templates themselves.
Let’s start with the alias templates.
With C++11, we got alias templates. Alias templates provide a means to give a convenient name to a family of types. The following code snippet presents the idea for the class template Matrix.
Matrix has three template parameters. The type parameter, and the non-type parameters, and
Col (I will write about template parameters in the next section.)
For readability, I want to have two special matrices: a
Square and a
Square‘s number of lines and columns should be equal. A
Vector‘s line size should be one. Thanks to type aliases, I can express my ideas directly in code.
using ((1) and (2)) declares a type alias. While the primary template
Matrix can be parametrized in the three dimensions
Col, the type aliases
Vector reduce the parametrization to the two dimensions
Line. From this point of view, alias templates enable it to create intuitive names for partially bound templates. Using
Vector is straightforward.
An excellent use case of alias templates is the type-traits library.
When you apply
std::move(arg) on a value
arg, the compiler uses typically
std::remove_reference to remove a reference from the underlying type:
Thanks to alias templates, version (line 2) have been valid since C++14. The following helper type is available:
Of course, the corresponding helper types for the other functions of the type-traits library returning a type are also available with C++14.
The previously defined class template
Matrix uses the two non-type template parameters
Template parameters can be types, non-types, and templates themselves.
Okay, types are the most often used template parameters. Here are a few examples:
Non-types can be a
- lvalue reference
- enumerator of a
- integral values
- floating-point values (C++20)
Integral values are the most used non-types. std::array is the typical example because you have to specify at compile time the size of a std::array:
Templates themself can be template parameters. Their definition may look a bit weird.
Matrix is a simple class template that can be initialized by a std::initializer_list (line 2). A Matrix can be used with a std::vector (line 4 and line 5) or a std::list (line 6) to hold its values. So far, nothing special.
But hold, I forget to mention lines 1 and 3. Line 1 declares a class template that has two template parameters. Okay, the first parameter is the type of the elements, and the second parameter stands for the container. Look at the second parameter: template <typename, typename> class Cont >. This means the second template argument should be a template requiring two template parameters. The first template parameter is the type of elements the container stores, and the second is the defaulted allocator a container of the standard template library has. Even the allocator has a default value, such as in the case of a std::vector. The allocator depends on the type of elements.
Line 3 shows the usage of the allocator in this internally used container. The matrix can use all containers, which are of the kind: container< type of the elements, allocator of the elements>. This is true for the sequence containers such as std::vector, std::deque, or std::list. std::array and std::forward_list would fail because std::array needs an additional non-type for specifying its size at compile-time and std::forward_list does not support the size method.
Maybe you don’t like the keyword class for the name of the template template parameter. With C++17, you can replace
Line (2) is valid since C++17 and is equivalent to line (1).
The Next pdf Bundle: Coroutines
In the post “Which pdf bundle do you want? Make your choice!” you decided on the coroutines bundle.
I’m still preparing the bundle, but it should be available in the next few days.
If you subscribe to the English newsletter, you automatically get the link to the current pdf bundle. Have a look at the top right corner of this page. This automatism makes it quite comfortable for me. People that are already subscribed to my newsletter get the link automatically.
In my next post, I will write about template arguments. It is pretty interesting how the compiler deduces the types. The rules do not only apply to function templates (C++98) but also to
auto (C++11), to class templates (C++17), and concepts (C++20).
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,