这个类是所有traits类的基类,分别提供了以下功能:
下面的代码分别来自C++11和Boost,略有差别:
// from c++11 standard
namespace std {
template <class T, T v>
struct integral_constant {
static constexpr 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 boost
template <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;
下面是调用代码,看看基本使用方法:
#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;
}
输出结果是:
one_t::value: 1 one_t::type::value: 1
原文:http://blog.csdn.net/csfreebird/article/details/44904121