Check Types

Contents[Show]

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;

}	


Thanks to the using of the flag std::boolalpha in line 22 the program display true or false instead of 1 or 0. Each call of the 14 primary type categories returns true.

primaryTypeCategories

How does the magic work?

The key of the technique 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.

 integral

Based on the 14 primary type categories there are 7 composite type categories in C++.

Composite type categories

 
CompositeTypeCategories

The is_fundamental type categorie 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 template like is_trivially_copyable have the name component trivially. That means that these methods have to be generated by the compiler and not by the developer. A method that 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, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Satish Vangipuram, and Michael Dunsky.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, and Said Mert Turkal.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

 

Comments   

0 #1 moved here 2016-11-29 06:00
This website was... how do I say it? Relevant!! Finally I have found something which helped me.
Cheers!
Quote
0 #2 binoy 2018-11-25 16:20
Please explain about the lines 13, 14 in integral.cpp. Thanks for your time.
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 25

Yesterday 7372

Week 29396

Month 34661

All 6892870

Currently are 182 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments