C++实战项目TinySTL之二:TypeTraits.h
程序员文章站
2022-05-24 18:49:14
...
最近看了《STL源码剖析》和侯捷老师的STL课程,打算做一个小型的STL,在增加项目经验的同时也顺带复习STL和数据结构相关的知识。整个系列完全参考岚岚路的博客和github上的一个STL项目项目地址
任务
STL中有多种形形色色的Traits,其中type_traits负责萃取类型特性,即某种类型是否具有琐碎无意义(trivial)的构造函数或复制控制,即POD类型(Plain Old Data)。
#ifndef _TYPE_TRAITS_H_
#define _TYPE_TRAITS_H_
namespace mySTL {
namespace {
template<bool, class Ta,class Tb>
struct IfThenElse;
template<class Ta,class Tb>
struct IfThenElse<true, Ta, Tb>
{
using result = Ta;
};
template<class Ta, class Tb>
struct IfThenElse<false, Ta, Tb>
{
using result = Tb;
};
}
}
struct _true_type{};
struct _false_type{};
//萃取传入的T类型的类型特性
template<class T>
struct _type_traits
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<bool>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<char>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<unsigned char>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<signed char>//在32位系统中一个char类型一般为8个bit,所以能存储的数据范围为-128~127,而unsigned char则是0~255,存储范围相同
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<wchar_t>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<short>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<unsigned short>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<int>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<unsigned int>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<long>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<unsigned long>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<long long>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<unsigned long long>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<double>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits< long double>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<class T>
struct _type_traits< T*>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<class T>
struct _type_traits<const T*>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits< char *>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<const char *>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<const unsigned char *>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
template<>
struct _type_traits<const signed char *>
{
typedef _false_type has_trivial_default_constructor;
typedef _false_type has_trivial_copy_constructor;
typedef _false_type has_trivial_assaignment_operator;
typedef _false_type has_trivial_destructor;
typedef _false_type is_POD_type;
};
#endif // ! _TYPE_TRAITS_H_
上一篇: tinystl实现(第三步:Construct.h)
下一篇: ehcache和quartz