samedi 18 juillet 2015

Moving to uninitialized memory, or how raw_storage_iterator works

I want to move a range of object into uninitialized memory (using move-construction). Since there is no move-counterpart to std::uninitialized_copy, i came up with two options: either use std::move with raw_storage_iterator, or resort to the manual loop:

T* dest = get_memory();
// option one
std::move(first, last, std::raw_storage_iterator<T*, T>(dest));
// option two
for (auto i=first; i != last; ++i, ++dest)
{
    new(dest) T(std::move(*i));
}

Will the first option do the move-construction (thus being equivalent to the second), or copy construction, or default construction followed by move assignment? Are there other considerations to prefer one option or another?

Aucun commentaire:

Enregistrer un commentaire