C++-----MySTL实现(3)---Traits编程技法

1、相关

        很多时候我们需要根据类型的不同来调用不同的函数,在析构和构造工具的实现中,destroy函数有两个版本,第二个版本的实现用到了value_type(first)来获取迭代器的类型,在内存处理工具中,也有很多地方用到这种方式来获得一些模板的类型。其实找到相对应的源码,会发现其实他们是通过模板的泛化、特化和偏特化实现的。关于特化、偏特化可以查看这篇C++ primer关于模板的那一章。在这一部分实现了STL中的type_traits.h文件,它主要的功能就是萃取出一些模板的类型,但是这个文件里没有实现有关迭代器的萃取,迭代器的萃取在iterator.h文件中实现。

2、代码

/*先看看书理解了萃取的机制,再实现
* 萃取传入的T类型的型别特性,包括:
* has_trivial_default_constructor;
* has_trivial_copy_constructor;
* has_trivial_assignment_operator;
* has_trivial_destructor;
* is_POD_type; (pod = plain old data)
*/
#ifndef TYPE_TRAITS_H_
#define TYPE_TRAITS_H_
namespace MySTL {
	struct __true_type {};
	struct __false_type {};

	//has nontrivial xxx...可以自己定义
	template <class _type>
	struct __type_traits
	{
		typedef __false_type    has_trivial_default_constructor;
		typedef __false_type    has_trivial_copy_constructor;
		typedef __false_type    has_trivial_assignment_operator;
		typedef __false_type    has_trivial_destructor;
		typedef __false_type    is_POD_type;  // 所谓 POD 意指 Plain Old Data structure. 	
	};

	//以下都是内置型别的特化
	template<>
	struct __type_traits<char>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<signed char>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<unsigned char>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<short>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<unsigned short>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<int>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<unsigned int>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<long>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<unsigned long>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<float>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<double>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	template<>
	struct __type_traits<long double>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

	//原生指针的特化,在标准库里是一个一个特化的。
	template <class _T>
	struct __type_traits<_T*>
	{
		typedef __true_type    has_trivial_default_constructor;
		typedef __true_type    has_trivial_copy_constructor;
		typedef __true_type    has_trivial_assignment_operator;
		typedef __true_type    has_trivial_destructor;
		typedef __true_type    is_POD_type;
	};

}
#endif

 

posted @ 2019-05-16 20:49  吾之求索  阅读(220)  评论(0)    收藏  举报