Template Metaprogramming is programming at compile time. But what has template metaprogramming in common with the type-traits library? A lot! The type-traits library is pure template metaprogramming, tamed in a library. With this post, my presentation of the type-traits library becomes more structured.
Check type properties
The type-trait library supports primary and composite type categories. You get the answer with the attribute value.
Primary type categories
C++ has 14 primary type categories. They are complete and orthogonal. This means that each type is exactly a member of one type category. The check for the type categories is independent of the type qualifiers const or volatile.
The 14 primary type categories:
And here is the application of the primary type categories:
Thanks to the use of the flag std::boolalpha in line 22, the program displays true or false instead of 1 or 0. Each call of the 14 primary type categories returns true.
How does the magic work?
The technique’s key is based on templates and template specialization, a few conventions, and a lot of typing. I wrote a possible implementation of the function template std::integral. std::integral will check if the type is integral.
I use in my implementation the namespace rgr and compare my implementation with type-traits implementation in the namespace std. The invocation of the function template rgr::is_integral<int>::value (line 69) causes under the hood the invocation of the expression rgr::true_type::value (line 77) because integral<int> is derived from true_type (line 42). rgr::true_type::value is an alias for rgr::integral_constant<bool, true>::value (line 17). I use only in the example the static constexpr value of the class integral_constant. integral_constant is the base class of the type-traits functions.
For completeness, the output of the program. My implementation behaves like the type-traits library.
Based on the 14 primary type categories, there are seven composite type categories in C++.
Composite type categories
The is_fundamental type category uses the function template is_same. More about I in the next post, in which I will write about type comparisons with the type-traits library.
There are more type checks possible with the type-traits.
In addition to the primary and composite type categories, you can check the type properties.
Many of the function templates like is_trivially_copyable, have the name component trivially. That means these methods must be generated by the compiler and not by the developer. A method you explicitly request from the compiler with the keyword default is also trivial.
The type-traits library has a lot to offer. I will write in the next post about type comparison and type modifications at compile time.
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,