The type-traits library is part of C++11 and supports type checks, type comparisons, and type modifications at compile time. The library has over 100 functions but grows with each new C++ standard release. Today, I present the type checks.
Each type belongs precisely to one of the fourteen primary type categories.
Primary Type Categories
Here are all of them:
The following program gives an example of a type fulfilling the check for each one of these primary type categories.
How Does this Magic Work?
This technique is based on templates and template specialization, a few conventions, and much typing. I wrote a simplified version of the function template
std::integral. std::integral to check if a given type is an integral type. I ignore
I use in my implementation the namespace
rgr and compare it with type-traits functions in the namespace
std. The invocation of the function template
rgr::is_integral<int>::value (1) causes the invocation of the expression
rgr::true_type::value (2) because
integral<int> is derived from
rgr::true_type::value is an alias for
rgr::integral_constant<bool, true>::value (2). I use in the example the static
constexpr value of the class
integral_constant is the base class of the type-traits functions.
For completeness, here is the output of the program. My implementation gives the same results, such as functions from the type-traits library.
I use in my function templates
::value as return. You maybe remember this convention from my previous post, “Template Metaprogramming – How it works“? Right! My function templates
rgr::is_integral are metafunctions and use template metaprogramming’s naming conventions. Since C++17, there is, for conventions, a helper class for
::value. This helper class is based on variable templates.
Variable templates are essentially a family of variables. Due to this helper class, you can write
std::integral_v<T> instead of
std::integral<T>::value. This shortened notation works for all function templates of the type traits library.
Composite type categories are then assembled from those primary type categories.
Composite Type Categories
There are seven composite type categories. The following table shows them.
In additionally, to the primary type categories and the composite type categories, the type traits library provides type properties and type property queries. For completeness, here are they.
Many of the meta-functions like
std::is_trivially_copyable have trivially in their name. That means that the compiler provides this method. Requesting a method from the compiler with the keyword
default is also trivial.
Type Property Queries
Did you notice the function
std::is_same in the composite type category s
std::is_same is it special because it provides type comparisons at compile time? I will write about type comparison at compile time in my next post.
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,