添加初始化和销毁对象的函数
This commit is contained in:
38
TinySTL/Construct.h
Normal file
38
TinySTL/Construct.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef _CONSTRUCT_H_
|
||||
#define _CONSTRUCT_H_
|
||||
|
||||
#include <new>
|
||||
|
||||
#include "TypeTraits.h"
|
||||
|
||||
namespace TinySTL{
|
||||
|
||||
template<class T1, class T2>
|
||||
inline void construct(T1 *ptr1, const T2& value){
|
||||
new(ptr1) T1(value);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void destroy(T *ptr){
|
||||
ptr->~T();
|
||||
}
|
||||
|
||||
template<class ForwardIterator>
|
||||
inline void _destroy(ForwardIterator first, ForwardIterator last, _true_type){}
|
||||
|
||||
template<class ForwardIterator>
|
||||
inline void _destroy(ForwardIterator first, ForwardIterator last, _false_type){
|
||||
for (; , first != last; ++first){
|
||||
destroy(&*first);
|
||||
}
|
||||
}
|
||||
|
||||
template<class ForwardIterator>
|
||||
inline void destroy(ForwardIterator first, ForwardIterator last){
|
||||
typedef typename _type_traits<T>::is_POD_type is_POD_type;
|
||||
_destroy(first, last, is_POD_type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -79,6 +79,7 @@
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Construct.h" />
|
||||
<ClInclude Include="TypeTraits.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
||||
@@ -23,5 +23,8 @@
|
||||
<ClInclude Include="TypeTraits.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Construct.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Construct.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
|
||||
Reference in New Issue
Block a user