My journey through programming at compile time began in the last posts with template metaprogramming. Today, I jumped from C++98 to C++11. This is a jump to the type-traits library which is template metaprogramming in a standardized way.
The type-traits library has been part of C++ since C++11. Its origin is in boost. The type traits support type checks, comparisons, and type modifications at compile time. The library has over 100 functions, but each C++ standard adds new ones.
I will not go into the details of the type-traits library because I have already written posts about it: type-traits library. On the contrary, I cannot skip the type-traits library to continue my introduction to programming at compile time. In the end, here is my short introduction to the type-traits library. For more information, I add links to my older posts.
First of all, what’s inside the type-traits library?
The Type-Traits Library
The library consists of type checks, type comparisons, and type modifications. Let me start with the type checks.
Each type belongs precisely to one primary type category.
Primary type categories
Here are they:
The following program gives you each primary type category a type.
Here is the output of the program:
If you want to know how this magic works, my post Check Types provides more information.
Based on the primary type categories are the composite type categories.
Composite Type Categories
The following table shows the relation between the primary type categories and the composite type categories.
There are more type checks possible with the type traits.
Many of the function templates like is_trivially_copyable have the name component trivially. That means that these methods are not implemented by you but by the compiler. Requesting a method from the compiler with the keyword default is also trivial.
The type-traits library supports three kinds of comparisons:
- is_base_of<Base, Derived>
- is_convertible<From, To>
- is_same<T, U>
The following example uses all three functions
and has the expected outcome.
Programming At Compile Time
Okay, let’s step back and think about the functions of the type-traits library. Here are a few observations.
- The functions from the type-traits library are metafunctions because they run at compile time. Metafunctions are class templates
- The metafunctions’ arguments that go into the sharp brackets (<…>) are metadata. Metadata are (in this case) types.
- The return value of the functions is the (::value). This is just an alias. Since C++17, there is a more straightforward form for getting the result: instead of std::is_void<void>::value, you type std::is_void_v<void>.
I hope these three observations remind you of my last post. These are precisely the conventions I presented in my previous post to template metaprogramming: C++ Core Guidelines: Programming at Compile Time.
If a function from the type-traits library wants to return a type, not a value, you must ask for it with ::type. My following post shows which type of modifications the type-traits library supports at compile time. Ultimately, the type-traits library has two goals: correctness and optimization.
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,