添加初始化和销毁对象的函数

This commit is contained in:
邹晓航
2014-09-17 09:20:34 +08:00
parent a60348ca81
commit dfe5081a99
4 changed files with 44 additions and 0 deletions

38
TinySTL/Construct.h Normal file
View 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

View File

@@ -79,6 +79,7 @@
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Construct.h" />
<ClInclude Include="TypeTraits.h" /> <ClInclude Include="TypeTraits.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -23,5 +23,8 @@
<ClInclude Include="TypeTraits.h"> <ClInclude Include="TypeTraits.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Construct.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,5 +1,7 @@
#include <iostream> #include <iostream>
#include "Construct.h"
using namespace std; using namespace std;
int main(){ int main(){