views:

56

answers:

3

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;
}
A: 

Vector elements are destructed in reverse order.

Steven Sudit
A: 

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.

AshleysBrain
+1  A: 

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.

UncleBens
Ah, `uninitialized_copy` is exactly what I was looking for. Thanks!
FredOverflow