1Integral_constant class
This class is the base class for all traits classes, and provides the following features, respectively:
- Value_type represents the type of value
- Value means
- Type represents itself, so you can use:: Type::value to get the value
- True_type and False_type Two special classes are used to represent the traits of a bool value type, and many traits classes need to inherit them
The following code comes from C++11 and boost, respectively, slightly different:
- C++11 contains the Value_type () function, which returns the true value
- C++11 using the CONSTEXPR keyword to indicate execution at compile time
From C++11 standardnamespace std { template <class T, t v> struct Integral_constant { static constexp R T value = V; typedef T VALUE_TYPE; typedef integral_constant<t,v> type; constexpr operator Value_type () {return value;} }; typedef integral_constant<bool, true> True_type; typedef integral_constant<bool, false> False_type;} From Boosttemplate <class T, t val>struct integral_constant{ typedef integral_constant<t, val> type; typedef T Value_type; static const T value = val;}; typedef integral_constant<bool, true> true_type;typedef integral_constant<bool, false> false_type;
Here is the calling code to see the basic use method:
#include <iostream> #include <type_traits>using std::cout;using std::endl;int main () { typedef std:: Integral_constant<int, 1> one_t; cout << "One_t::value:" << one_t::value << Endl; cout << "One_t::type::value:" << one_t::type::value << Endl;}
The output is:
One_t::value:1one_t::type::value:1
C + + template meta-Programming VI: Integral_constant class