一、类型萃取实现对象构建、析构

(1)Object.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef OBJECT_H
#define OBJECT_H
#include <iostream>
using namespace std;

#if 1
template<class _Ty>
class Object
{
private:
_Ty _val;
public:

Object(const _Ty& val = 0) : _val(val)
{
cout << "Object: " << _val << endl;
}
~Object()
{
cout << "~Object()"<< endl;
}
};

#endif
#endif

(2)my_iterator.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef MY_ITERATOR_H
#define MY_ITERATOR_H
namespace pzj
{
//只读迭代器标记
class input_iterator_tag {};
//只写迭代器标记
class output_iterator_tag {};

//正向迭代器标记
class forward_iterator_tag : public input_iterator_tag {};
//双向迭代器标记
class bidirectional_iterator_tag : public forward_iterator_tag {};
//随机迭代器标记
class random_iterator_tag : public bidirectional_iterator_tag {};

/*-----------------------------------------------------------*/
//指针差值类型
typedef int ptrdiff_t;

//全局迭代器iterator
template<class _C, class _Ty,class _D = ptrdiff_t, class _Pointer = _Ty*, class _Reference = _Ty&>
struct iterator
{
typedef _C iterator_category; //迭代器类别
typedef _Ty value_type; //数值类型
typedef _D difference_type; //差值类型
typedef _Pointer pointer; //数值指针类型
typedef _Reference reference; //值引用类型
};

//正向迭代器
template<class _Ty, class _D>
struct _Forit : public iterator<forward_iterator_tag, _Ty, _D>
{};
//双向迭代器
template<class _Ty, class _D>
struct _Bidit : public iterator<bidirectional_iterator_tag, _Ty,_D>
{};
//随机迭代器
template<class _Ty, class _D>
struct _Randit : public iterator<random_iterator_tag, _Ty, _D>
{};

//迭代器类型萃取类
template<class _Iterator>
class iterator_traits
{
public:
//构造函数
iterator_traits() {}
typedef typename _Iterator::iterator_category iterator_category; //迭代器类别
typedef typename _Iterator::value_type value_type; //数值类型
typedef typename _Iterator::difference_type difference_type; //差值类型
typedef typename _Iterator::pointer pointer; //数值指针类型
typedef typename _Iterator::reference reference; //值引用类型
};
//迭代器类型萃取类<普通指针>:可以看作随机迭代器
template<class T>
class iterator_traits<T*>
{
public:
//构造函数
iterator_traits() {}
typedef typename random_iterator_tag iterator_category; //迭代器类别
typedef typename T value_type; //数值类型
typedef typename int difference_type; //差值类型
typedef typename T* pointer; //数值指针类型
typedef typename T& reference; //值引用类型
};
//迭代器类型萃取类<常性普通指针>:可以看作随机迭代器
template<class T>
class iterator_traits<const T*>
{
public:
//构造函数
iterator_traits() {}
typedef typename random_iterator_tag iterator_category; //迭代器类别
typedef typename T value_type; //数值类型
typedef typename int difference_type; //差值类型
typedef typename const T* pointer; //数值指针类型
typedef typename const T& reference; //值引用类型
};

//返回迭代器类别
template<class _It>
inline typename iterator_traits<_It>::iterator_category
Iterator_category(_It it)
{
typedef typename iterator_traits<_It>::iterator_category iter_cate;
return iter_cate();
}
//返回value_type类别
template<class _It>
inline typename iterator_traits<_It>::value_type*
value_type(_It it)
{
typedef typename iterator_traits<_It>::value_type* _pointer;
return static_cast<_pointer>(0);
}

//返回different_type类别
template<class _It>
inline typename iterator_traits<_It>::different_type*
different_type(_It it)
{
typedef typename iterator_traits<_It>::different_type* _different_type;
return static_cast<_different_type>(0);
}
/*-----------------------------------------------------------*/
//input、output、forward迭代器都使用该迭代方式
template<class _II, class _D>
inline void __advance(_II& it, _D n, input_iterator_tag)
{
/* while (n--)
{
++it;
}*/
}
//双向迭代器的迭代方式
template<class _BI, class _D>
inline void __advance(_BI& it, _D n, bidirectional_iterator_tag)
{
/*if (n > 0)
{
while(n--) ++it;
}
else
{
while (n++) --it;
}*/
}
//随机迭代器的迭代方式
template<class _RI, class _D>
inline void __advance(_RI& it, _D n, random_iterator_tag)
{
//it += n;
}

//统一接口
template<class _II, class _D>
inline void advance(_II& it, _D n)
{
//由于未知迭代器的类别标志,所以暂时无法使用__advance()调用对应的函数
//__advance(it, n, 迭代器类型标志);

//类型萃取出_II的迭代器类型iter_cate
typedef typename iterator_traits<_II>::iterator_category iter_cate;
__advance(it, n, iter_cate());
}

//实现distance
//输入、输出、正向、双向
template<class _II>
inline typename iterator_traits<_II>::difference_type
__distance(_II first, _II last, input_iterator_tag)
{
typename iterator_traits<_II>::difference_type _dist = 0;
while (first != last)
{
++first;
++_dist;
}
return _dist;
}
//随机
template<class _RAI>
inline typename iterator_traits<_RAI>::difference_type
__distance(_RAI first, _RAI last, random_iterator_tag)
{
return last - first;
}

//统一接口
template< class InputIt >
typename iterator_traits<InputIt>::difference_type
distance(InputIt first, InputIt last)
{
//typedef typename iterator_traits<InputIt>::iterator_category iter_cate;
//__distance(first, last, iter_cate());
__distance(first, last, Iterator_category(first));
}
}
#endif

(3)my_type_traits.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef MY_TYPE_TRAITS_H
#define MY_TYPE_TRAITS_H
namespace pzj
{
struct __true_type {};
struct __false_type {};


template<class type>
struct __type_traits
{
typedef __true_type this_dummy_member_must_be_first;
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; // struct int
};


//内置类型
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; // struct int
};

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; // struct int
};

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; // struct int
};

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; // struct int
};

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; // struct int
};
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; // struct int
};
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; // struct int
};
template<>
struct __type_traits<long 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; // struct int
};
template<>
struct __type_traits<unsigned long 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; // struct int
};

template<>
struct __type_traits<long 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; // struct int
};
template<>
struct __type_traits<unsigned long 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; // struct int
};
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; // struct int
};

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; // struct int
};

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; // struct int
};

//所有指针类型
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; // struct int
};
}

#endif

(4)my_construct.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef MY_CONSTRUCT_H
#define MY_CONSTRUCT_H
#include "my_iterator.h"
#include "my_type_traits.h"
#include <iostream>

namespace pzj
{
//默认构造
template<class _Ty>
inline void construct(_Ty* p)
{
new(p) _Ty();
}
//有参构造
template<class _Ty, class _Vt>
inline void construct(_Ty* p, const _Vt& val)
{
new(p) _Vt(val);
}

//析构
//普通指针析构统一接口
template<class _Ty>
inline void destroy(_Ty* p)
{
p->~_Ty();
}

//迭代器析构对象
//没必要进行析构的对象
//三级接口1
template<class _It>
inline void __destroy_aux(_It first, _It last, pzj::__true_type)
{
std::cout << "destroy none do!"<< std::endl;
}

//三级接口2
//有必要进行析构的对象
template<class _It>
inline void __destroy_aux(_It first, _It last, pzj::__false_type)
{
while (first != last)
{
destroy(&*first);
++first;
}
}

//二级接口
template<class _It, class _Ty>
inline void __destroy(_It first, _It last, _Ty*)
{
//萃取_Ty类型是否是无关紧要类型
typedef typename __type_traits<_Ty>::has_trivial_destructor dest;
__destroy_aux(first, last, dest());
}

//一级统一接口
template<class _It>
inline void destroy(_It first, _It last)
{
//萃取_It管理的对象类型
//typedef typename iterator_traits<_It>::value_type value_type;
//__destroy(first, last, value_type());

//调用my_iterator.h文件中value_type函数
__destroy(first, last, pzj::value_type(first));
}
}
#endif

二、construct、destroy测试模块

(1)有关紧要的析构

Demon.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "my_construct.h"
#include "Object.h"

//construct、destroy测试模块
#if 1
int main()
{
const int n = 10;
Object<int>* p = (Object<int>*)malloc(sizeof(Object<int>) * n);
//Object<int> obj(10);
for (int i = 0; i < n; ++i)
{
pzj::construct(&p[i]);
}

pzj::destroy(p, p + n);
free(p);
return 0;
}
#endif

在这里插入图片描述

(2)无关紧要的析构

Demon.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "my_construct.h"
#include "Object.h"

//construct、destroy模块
#if 1
template<>
struct pzj::__type_traits<Object<int>>
{
typedef __true_type this_dummy_member_must_be_first;
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor; //析构无关紧要
typedef __false_type is_POD_type; //是一个 只有值的集合类型
};


int main()
{
const int n = 10;
Object<int>* p = (Object<int>*)malloc(sizeof(Object<int>) * n);
//Object<int> obj(10);
for (int i = 0; i < n; ++i)
{
pzj::construct(&p[i]);
}

pzj::destroy(p, p + n);
free(p);
return 0;
}

在这里插入图片描述