Question

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;
}
Was it helpful?

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.

OTHER TIPS

Vector elements are destructed in reverse order.

The STL relies on allocators (passed as a default template parameter in STL containers) which have methods construct and destroy and others like uninitialized_fill, but not all of them operate on ranges. The allocator, not the container, are responsible for providing these functions. If you want to write your own vector class like the STL, you'll also need to reinvent allocators.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top