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. These means that each type is exactly 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 displays 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 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 templates 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.

 

 

 

 

 

 

 

title page smalltitle page small Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library".   Get your e-book. Support my blog.

 

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

Add comment


My Newest E-Book

Latest comments

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 91

All 333382

Currently are 159 guests and no members online