STL-Object Construction and Destruction

Keywords: C++ github

The configuration/release of STL memory space and the construction/destruction of object content are done separately.
 

Construction and Destruction of Objects

The construction of the object is done by the construct function, which calls the locate new operator internally to construct the object in the specified memory location.The following:
template <typename T1, typename T2>
inline void construct(T1 *p, const T2& value)
{
        //Location new
        new (p) T1(value);
}

 

Destruction of objects is done by the destroy function, which has two versions: one pointer and two iterators.A version that accepts a pointer directly calls the destructor of the object pointed to by the pointer to complete the destruction; a version that receives two iterators determines whether the destroy of the object type pointed by the iterator is trivial, if it is, does nothing, or if it is not, traverses the range specified by the two iterators and calls the first version of destroy one by one.This is done primarily for efficiency in destructive purposes.
* The following is the first version of destroy
//destroy()Version 1, accepts a pointer
template <typename T>
inline void destroy(T* point)
{
        point->~T();
}
* The following illustration shows version 2 destroy
//Yes trivial destructor
template <typename Iterator>
inline void __destroy_aux(Iterator first, Iterator last, __true_type) {}

//Yes non-trivial destructor
template <typename Iterator>
inline void __destroy_aux(Iterator first, Iterator last, __false_type)
{
        for (; first < last; first++)
                destroy(&*first);  //Call first version destroy()
}

//Determine if an element has a value type trivial destructor
template <typename Iterator, typename T>
inline void __destroy(Iterator first, Iterator last, T* )
{
        typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
        __destroy_aux(first, last, trivial_destructor());
}

//destroy()Version 2, accepting two iterators
template <typename Iterator>
inline void destroy(Iterator first, Iterator last)
{
        __destroy(first, last, value_type(first));
}

 

In this case, the object type referred to by the iterator is determined by the iterator_traits template structure, which uses the parameter inference function of the template to infer the object type referred to by the iterator.As shown in the following figure:
//Returns the object type pointed to by the iterator value_type*
template <typename Iterator>
inline typename iterator_traits<Iterator>::value_type*
value_type(const Iterator&)
{
        return static_cast<typename iterator_traits<Iterator>::value_type*>(NULL);
}

 

//iterator traits
template <typename Iterator>
struct 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;
};

 

The source code in this article comes from my MiniSTL project, which is an implementation of a simple version of STL.
Project Address: https://github.com/zinx2016/MiniSTL/tree/master/MiniSTL
 
(full text complete)

Posted by pmjm1 on Fri, 14 Jun 2019 10:23:59 -0700