A variadic template is a template that can have an arbitrary number of template parameters. This feature may seem magical to you if you see it the first time. So, let me demystify variadic templates.
You may wonder that my graphic showing the topics I write about includes template instantiation. The reason is simple. After my last post about "Template Instantiation", one of my German readers (pseudonym Urfahraner Auge) made a comment. There is an important difference between implicit and explicit instantiation of a template that I forgot to mention. He is right. The implicit instantiation of templates is lazy but the explicit instantiation of templates is eager.
Lazy versus Eager Template Instantiation
Template instantiation is lazy.
Meaning, if you don't need a member function of a class template it will not be instantiated. Only the declaration of the member function is available, but not its definition. This works so far that you can use invalid code in a member function. Of course, the member function must not be called.
If you call the member function
numb.absValue() (line 1), you get what you may expect. A compile-time error message essentially saying that the is no overload
std::string available. Here are the first two lines from the verbose error message:
I have to explain template instantiation more precisely: The implicit instantiation of templates is lazy but the explicit instantiation of templates is eager.
When you enable line (2) (
template class Number<std::string>) and explicitly instantiated the class template
Number or you enable line (3) (
template int Number<std::string>::absValue()) and explicitly instantiated the member function
std::string, you get a compile-time error. This compile-time error is equivalent to the compiler error invoking the member function absValue in line (1) (
numb.absValue()). Once more, here are the first two lines of the error messages after enabling line (2) or line (3).
- Line (2) enabled
- Line (3) enabled
A Personal Note:
I'm keen on getting comments about my posts. They help me to write about the content you want to hear. In particular, the German community is very engaged.
Now, finally to something completely different: variadic templates.
A variadic template is a template that can have an arbitrary number of template parameters. This feature may seem magical to you if you see it the first time.
The ellipsis (
args a so-called parameter pack. Precisely,
Args is a template parameter pack and
args is a function parameter pack. Two operations are possible with parameter packs. They can be packed and unpacked. If the ellipse is to the left of
Args, the parameter pack will be packed, if it is to the to the right of
Args, it is unpacked. Because of the function template argument deduction, the compiler can derive the template arguments.
Variadic templates are often used in the Standard Template Library and also in the core language.
All four examples from the C++11 standard use variadic templates. The first three are part of the Standard Template Library. Let's see what I can deduce from the declarations.
std::tupleaccepts an arbitrary number of different types.
std::threadallows it to invoke a callable with an arbitrary number of arguments. The argument can have different types. A callable is something you can invoke such as a function, a function object, or a lambda expression. The function
std::threadtakes its callable and its arguments by universal reference. If you need more detail: I already wrote about template argument deduction and universal references in my post "Template Arguments".
std::lockallows it to lock an arbitrary number of lockable types in an atomic step. Locking one lockable type in an atomic step is trivial. Consequently,
std::lockrequires at least two arguments.
Lockableis named requirement. Types supporting
Lockablemust have the member functions
sizeof ...- operator returns the number of elements in the
sizeof...-operator seems to be special because the ParameterPack is used in the core language. Let me write a few words about it.
Thanks to the
sizeof ...-operator can be used to directly determine how many elements a parameter pack contains. The elements are not evaluated.
sizeof..-operator allows it to determine the size of the template parameter pack (1) and the function parameter pack (2) at compile time. I apply it to an empty parameter pack (3), and a parameter pack containing four elements. The first element is a C-string and the second a C++-string. To use the C++-string literal, I have to include the namespace
std::literals (5). C++14 supports C++ string literals.
In my next post, I dive deeper into variadic templates and introduce the functional pattern to evaluate a variadic template. Additionally, I present the perfect factory function and jump from C++11 to C++17: fold expression in C++17.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Red Trip, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Haken Gedek, Bernd Mühlhaus, Challanger, Matthieu Bolt, Stephen Kelley, and Kyle Dean.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, and John Nebel.
My special thanks to Embarcadero
My special thanks to PVS-Studio
My new mentoring program "Fundamentals for C++ Professionals" starts in April. Get more information here: https://bit.ly/MentoringProgramModernesCpp.
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++: 28.06.2022 - 30.06.2022
- C++20: 23.08.2022 - 25.08.2022
- Clean Code: Best Practices für modernes C++: 11.10.2022 - 13.10.2022
- Design Pattern und Architekturpattern mit C++: 08.11.2022 - 10.11.2022
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 Patterns and Architecture Patterns with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net