Metaprogramming is programming on programs. C++ applies metaprogramming at compile time. It started in C++98 with template metaprogramming, was formalized in C++11 with the type-traits library, and since C++11 has steadily improved. The main driving force is constant expressions. In this post, I want to write about its roots.
In writing about template metaprogramming, I want to demystify its techniques. This demystification helps you to understand better the functions of the type-traits library and, in particular, appreciate
constexpr. Most of the bad reputation of template metaprogramming is that you may get error messages of epic length. Template metaprogramming was not designed; it started with an accident.
In 1994, Erwin Unruh from Siemens presented at a C++ committee meeting a program that didn’t compile. Here is probably the most famous program that never compiled successfully.
Erwin Unruh used the Metaware Compilers, but the program is not valid for C++ anymore. A newer variant from the author is here. Okay, why is this program so famous? Let’s look at the original error messages that wrote type as txpe.
I highlighted the important parts in red. I think you see the pattern. The program calculates at compile time the first prime numbers until 30. This means template instantiation can be used to do math at compile time. It is even better. Template metaprogramming is Turing-complete, and can, therefore, be used to solve any computational problem. (Of course, Turing completeness holds only in theory for template metaprogramming because the recursion instantiation depth (at least 1024 with C++11) and the length of the names generated during template instantiation provide some limitations.)
How does the magic work?
Let me decompose what is going on step by step.
Calculating at Compile Time
Calculating the factorial of a number is the “Hello World” of template metaprogramming.
factorial<5>::value in line (1) causes the instantiation of the primary or general template in line (2). During this instantiation,
Factorial<4>::value will be instantiated. This recursion will end if the fully specialized class template
Factorial<1> kicks in in line (3). Maybe, you like it more pictorial.
Here is the output of the program:
Let me start with C++ Insights:
Factorial<5>::value (line 1) causes the instantiation of the class template for the numbers 5 to 2. The full specialization for 1 is already available. The call
Factorial<10>::value (line 2) causes the instantiation of the function template for the numbers 10 – 6 because all other fully specialized function templates are already available. The following output shows the instantiation for the numbers 5 to 2.
Now, my analysis continues with the Compiler Explorer.
For simplicity reasons, I only provide a screenshot of the main program and the corresponding assembler instructions.
The Compiler Explorer allows you to visualize this compile-time calculation.
The output shows it. The factorials of 5 and 10 are just constants and were calculated during compile time. You can see the result directly in the first line and last line of the assembler instructions.
I was quite happy this week that I could use a previous post as a starting point for this post. I gave this week four talks at the CppCon and honestly, this was too much. Here are my talks that are published on Youtube’s CppCon channel. The pdfs are already available.
- Concurrency Patterns
- The Many Flavors of Constness in Modern C++
- Object-Oriented Programming: The Good Parts
- C++20: The Small Pearls
In my next post, I will continue my journey with template metaprogramming and provide more insights.
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, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
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|
|My special thanks to SHAVEDYAKS|
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,