fredoverflow
fredoverflow

Reputation: 263390

Is there an algorithm for moving ranges?

In C++98, I can copy ranges with the std::copy algorithm.

std::copy(source.begin(), source.end(), destination.begin());

Is there an algorithm in C++0x that moves the elements from source to destination? Or is std::copy somehow overloaded to accept something like rvalue iterators -- is there even such a thing?

The algorithm might look something like this:

#include <utility>

template<class InputIterator, class OutputIterator>
OutputIterator mooove(InputIterator first, InputIterator last, OutputIterator result)
{
    for (; first != last; ++first, ++last) *result = std::move(*first);
    return result;
}

Upvotes: 5

Views: 777

Answers (3)

mtmk
mtmk

Reputation: 6336

It seems to be in the latest draft (see section 25.3.2).

I have a hard copy of C++03 which is exactly the same as C++98 (sections 25.2.x) where you can see the same algorithms (without 'move' obviously).

Upvotes: 5

There is also an iterator adaptor, std::move_iterator, that can be used to adapt any range algorithm that makes copies to move the elements instead. std::move(first, last, dest) is just a convenience wrapper for the most common use case -- it is semantically equivalent to std::copy(std::move_iterator(first), std::move_iterator(last), dest).

Upvotes: 2

Potatoswatter
Potatoswatter

Reputation: 137960

Just because this page is a lil confusing right now… There is a std::move( first, last, dest ), OP was just confused by looking at an older C++0x draft. The function is defined by section 25.3.2.

Upvotes: 0

Related Questions