Check Types
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:
template <class T> struct is_void; template <class T> struct is_integral; template <class T> struct is_floating_point; template <class T> struct is_array; template <class T> struct is_pointer; template <class T> struct is_reference; template <class T> struct is_member_object_pointer; template <class T> struct is_member_function_pointer; template <class T> struct is_enum; template <class T> struct is_union; template <class T> struct is_class; template <class T> struct is_function; template <class T> struct is_lvalue_reference; template <class T> struct is_rvalue_reference;
And here is the application of the primary type categories:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// primaryTypeCategories.cpp #include <iostream> #include <type_traits> struct A{ int a; int f(int){return 2011;} }; enum E{ e= 1, }; union U{ int u; }; int main(){ std::cout << std::boolalpha << std::endl; std::cout << std::is_void<void>::value << std::endl; std::cout << std::is_integral<short>::value << std::endl; std::cout << std::is_floating_point<double>::value << std::endl; std::cout << std::is_array<int [] >::value << std::endl; std::cout << std::is_pointer<int*>::value << std::endl; std::cout << std::is_reference<int&>::value << std::endl; std::cout << std::is_member_object_pointer<int A::*>::value << std::endl; std::cout << std::is_member_function_pointer<int (A::*)(int)>::value << std::endl; std::cout << std::is_enum<E>::value << std::endl; std::cout << std::is_union<U>::value << std::endl; std::cout << std::is_class<std::string>::value << std::endl; std::cout << std::is_function<int * (double)>::value << std::endl; std::cout << std::is_lvalue_reference<int&>::value << std::endl; std::cout << std::is_rvalue_reference<int&&>::value << std::endl; std::cout << std::endl; } |
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
// integral.cpp #include <iostream> #include <type_traits> namespace rgr{ template<class T, T v> struct integral_constant { static constexpr T value = v; typedef T value_type; typedef integral_constant type; constexpr operator value_type() const noexcept { return value; } constexpr value_type operator()() const noexcept { return value; } //since c++14 }; typedef integral_constant<bool, true> true_type; typedef integral_constant<bool, false> false_type; template <class T> struct is_integral : public false_type{}; template <> struct is_integral<bool> : public true_type{}; template <> struct is_integral<char> : public true_type{}; template <> struct is_integral<signed char> : public true_type{}; template <> struct is_integral<unsigned char> : public true_type{}; template <> struct is_integral<wchar_t> : public true_type{}; template <> struct is_integral<short> : public true_type{}; template <> struct is_integral<int> : public true_type{}; template <> struct is_integral<long> : public true_type{}; template <> struct is_integral<long long> : public true_type{}; template <> struct is_integral<unsigned short> : public true_type{}; template <> struct is_integral<unsigned int> : public true_type{}; template <> struct is_integral<unsigned long> : public true_type{}; template <> struct is_integral<unsigned long long> : public true_type{}; } int main(){ std::cout << std::boolalpha << std::endl; std::cout << "std::is_integral<int>::value: " << std::is_integral<int>::value << std::endl; std::cout << "rgr::is_integral<int>::value: " << rgr::is_integral<int>::value << std::endl; std::cout << "std::is_integral<double>::value: " << std::is_integral<double>::value << std::endl; std::cout << "rgr::is_integral<double>::value: " << rgr::is_integral<double>::value << std::endl; std::cout << std::endl; std::cout << "std::true_type::value: " << std::true_type::value << std::endl; std::cout << "rgr::true_type::value: " << rgr::true_type::value << std::endl; std::cout << "std::false_type::value: " << std::false_type::value << std::endl; std::cout << "rgr::false_type::value: " << rgr::false_type::value << std::endl; std::cout << std::endl; std::cout << "std::integral_constant<bool, true>::value: " << std::integral_constant<bool, true>::value << std::endl; std::cout << "rgr::integral_constant<bool, true>::value: " << rgr::integral_constant<bool, true>::value << std::endl; std::cout << "std::integral_constant<bool, false>::value: " << std::integral_constant<bool, false>::value << std::endl; std::cout << "rgr::integral_constant<bool, false>::value: " << rgr::integral_constant<bool, false>::value << std::endl; std::cout << std::endl; } |
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.
Type properties
In addition to the primary and composite type categories, you can check the type properties.
template <class T> struct is_const; template <class T> struct is_volatile; template <class T> struct is_trivial; template <class T> struct is_trivially_copyable; template <class T> struct is_standard_layout; template <class T> struct is_pod; template <class T> struct is_literal_type; template <class T> struct is_empty; template <class T> struct is_polymorphic; template <class T> struct is_abstract; template <class T> struct is_signed; template <class T> struct is_unsigned; template <class T, class... Args> struct is_constructible; template <class T> struct is_default_constructible; template <class T> struct is_copy_constructible; template <class T> struct is_move_constructible; template <class T, class U> struct is_assignable; template <class T> struct is_copy_assignable; template <class T> struct is_move_assignable; template <class T> struct is_destructible; template <class T, class... Args> struct is_trivially_constructible; template <class T> struct is_trivially_default_constructible; template <class T> struct is_trivially_copy_constructible; template <class T> struct is_trivially_move_constructible; template <class T, class U> struct is_trivially_assignable; template <class T> struct is_trivially_copy_assignable; template <class T> struct is_trivially_move_assignable; template <class T> struct is_trivially_destructible; template <class T, class... Args> struct is_nothrow_constructible; template <class T> struct is_nothrow_default_constructible; template <class T> struct is_nothrow_copy_constructible; template <class T> struct is_nothrow_move_constructible; template <class T, class U> struct is_nothrow_assignable; template <class T> struct is_nothrow_copy_assignable; template <class T> struct is_nothrow_move_assignable; template <class T> struct is_nothrow_destructible; template <class T> struct has_virtual_destructor;
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.
What’s next?
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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.
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 |
Modernes C++ GmbH
Modernes C++ Mentoring (English)
Rainer Grimm
Yalovastraße 20
72108 Rottenburg
Mail: schulung@ModernesCpp.de
Mentoring: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!