tinystl実装(ステップ3:Construct.h)
長い間勉強してやっとtinystlの模倣(chao)の仕事を始めることができて、本文はこの大物のgithubを参考にして、率直に言って私はただ注釈を補充しただけで、tinystlのコードは本当に経典で、私はこのような大型プロジェクトの経験がないので、このようにするしかありません.しかし、皆さんの勉強に役立つと信じています.このコラムを順番に読むことを強くお勧めします.hファイルは私たちのコンテナの構造と分析を担当して、上はコンテナにメモリを提供してコンテナのインタフェースを破壊して、下はメモリとフックを割り当てて、その文の間はc++文法の強大さを明らかにして、とても面白いコードです
#pragma once
#ifdef _CONSTRUCT_H_
#define _CONSTRUCT_H_
#include
#include"TypeTrails.h"
namespace mySTL {
template<class T1,class T2>
inline void construct(T1 *ptr1, const T2& value) {
new(ptr1) T1(value);// new
}
template<class T>
inline void destroy(T *ptr) {
ptr->~T();//
}
template<class ForwardIterator>
inline void _destroy(ForwardIterator first,ForwardItrator last,_true_type){}
// pod , , ,
template<class ForwardIterator>
inline void _destroy(ForwardIterator first, ForwardItrator last, _false_type) {
for (; first != last; ++first) {
destroy(&*first);// ( )
}
}
// pod , destroy
template<class ForwardIterator>
inline void _destroy(ForwardIterator first, ForwardItrator last) {
typedef typename _type_traits<ForwardIterator>::is_POD_type is_POD_type;// ForwardIterator is_POD_type
_destroy(first, last, is_POD_type());//
}
// pod , ( )
}// inline , , ,
#endif // _CONSTRUCT_H_