type_traits
type_traits是C++11提供的模板元基础库。
type_traits可实现在编译期计算、判断、转换、查询等等功能。
type_traits提供了编译期的true和false。
// type_traits中源码
//is_const的实现
//以下为了和实际源代码区分,全部加了my
template<typename _Tp>
struct my_is_const
: public my_false_type
{ }; // 泛化版本
template<typename _Tp>
struct my_is_const<_Tp const>
: public my_true_type
{ }; // 偏特化版本
// integral_constant
template<typename _Tp, _Tp __v>
struct my_integral_constant
{
static constexpr _Tp value = __v;//静态数据成员
typedef _Tp value_type;
typedef my_integral_constant<_Tp, __v> type;
};
typedef my_integral_constant<bool, true> my_true_type;
typedef my_integral_constant<bool, false> my_false_type;
int main()
{
cout << my_is_const<int>::value << endl;//0
cout << is_const<int>::value << endl;//0
cout << my_is_const<const int>::value << endl;//1
cout << is_const<const int>::value << endl;//1
system("pause");
return 0;
}