经过长时间的学习终于可以开始tinystl的仿(chao)写工作了,本文参考了这位大佬的github,坦白讲我只是补充了注释,因为tinystl的代码真的非常经典而我又没什么这种大型项目的经验,所以只能这样做,不过相信能够有助于大家的学习
TypeTraits.h文件其实应该是最基础的部分,这部分完成了很多以后要用到的东西的定义,理解比较困难,可以先不理解,以后回头就会发现这些设置有何用处
阅读本文你需要:
了解SFINAE:通过这篇文章了解
了解类型萃取:通过这篇文章
了解何为pod类型:通过这篇文章
#pragma once
#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;
};
}//对namespace的定义
}
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>//wchar_t的表示范围远远大于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<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_