Today, I will continue my introduction to programming at compile time. The last post started with template metaprogramming. Here is where I pick up today and finish.
Here is the big picture, before I dive in.
Only to remind you, the rules of the C++ Core Guidelines were my starting point:
- T.120: Use template metaprogramming only when you really need to
- T.121: Use template metaprogramming primarily to emulate concepts
- T.122: Use templates (usually template aliases) to compute types at compile time
- T.123: Use
constexprfunctions to compute values at compile time
- T.124: Prefer to use standard-library TMP facilities
- T.125: If you need to go beyond the standard-library TMP facilities, use an existing library
We are just at the bottom of the triangle.
In the last post C++ Core Guidelines: Rules for Template Metaprogramming, I present a short template metaprogram that removed the constness from its arguments.
What can we learn from this example?
- Template specialization (partial or full) is a compile-time if. More specifically, when I use removeConst with a non-constant int (line 1), the compiler chooses the primary or general template (line 2). When I use a constant int (line 3), the compiler chooses the partial specialization for const T (line 4).
- The expression typedef T type serves as a return value which is, in this case, a type.
Now, it has become funny.
At runtime, we use data and functions. At compile time, we use metadata and metafunctions. Relatively easy, it’s called meta because we do metaprogramming, but what is metadata or a metafunction? Here is the first definition.
- Metadata: Types and integral values that are used in metafunctions.
- Metafunction: Functions that are executed at a compile-time.
Let me elaborate more on the terms metadata and metafunction.
Metadata includes three entities:
- Types such as int, or double.
- Non-types such as integrals, enumerators, pointers, or references
- Templates such as std::stack
As I mentioned, I use only types and integrals in my examples of Template Metaprogramming.
Of course, this sounds strange. Types are used in template metaprogramming to simulate functions. Based on my definition of metafunctions, constexpr functions which can be executed at compile time, are also metafunctions, but this is my topic for a later post.
Here are two types you already know from my last post C++ Core Guidelines: Rules for Template Metaprogramming: Factorial and RemoveConst.
The first metafunction returns a value, and the second a type. The name value and type are just naming conventions for the return values. If a metafunction returns a value, it is called a value; if it returns a type, it is called a type. The type traits library to which I come in my next post follows exactly this naming convention
I think it is pretty enlightening to compare functions with metafunctions.
Functions versus Metafunctions
The following function power and the meta function Power calculate pow(2, 10) at runtime and compile time.
Here are the first differences:
- Arguments: The function arguments go into the round brackets (“( … )” in line A)), and the metafunction arguments go into the sharp brackets (“< …>” in line B). This also holds for the definition of the function and the metafunction. The function uses round brackets and metafunction sharp brackets.
- Return value: The function uses a return statement (line 3), and the metafunction is the static integral constant value.
I will continue this comparison when I come to constexpr functions. Anyway, here is the output of the program.
This was easy! Right? power is executed at runtime and Power at compile time, but what is happening here?
The call power<10>(2) inline (1) uses sharp and round brackets and calculates 2 to the power of 10. This means, 10 is the compile time and 2 is the runtime argument. To say indifferently: power is a function and a metafunction. Now, I can instantiate the class template for 2 and give it the name power2 (line 2). CppInsight shows me that the compiler is instantiating power for 2. Just the function argument is not bound.
The function argument is a runtime argument and can be used in a for loop (line 3).
In the next post, I will jump to the next level of the triangle and write about the type traits library. Template metaprogramming has been available in C++ since C++98, but the type traits since C++11. You may already assume it. The type-traits library is just a cultivated form of template metaprogramming.
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,