Search code examples
c++algorithmtemplatesstlobject-lifetime

algorithms that destruct and copy_construct


I am currently building my own toy vector for fun, and I was wondering if there is something like the following in the current or next standard or in Boost?

template<class T>
void destruct(T* begin, T* end)
{
    while (begin != end)
    {
        begin -> ~T();
        ++begin;
    }
}

template<class T>
T* copy_construct(T* begin, T* end, T* dst)
{
    while (begin != end)
    {
        new(dst) T(*begin);
        ++begin;
        ++dst;
    }
    return dst;
}

Solution

  • std::vector, if I'm not mistaken, applies its allocator's construct and destruct functions on individual items, so you could also use binders (like std::tr1::bind) to let std::transform and/or std::for_each do those.

    But for the copying loop, there also appears to be std::uninitialized_copy.