tags:

views:

179

answers:

3

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

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).

Maxwell Troy Milton King
Ah, found it, thank you. It's actually 25.3.2 ;)
FredOverflow
A: 

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.

Potatoswatter
+1  A: 

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).

Johannes Dahlström
Ah, so there is indeed such a thing as an "rvalue iterator" :)
FredOverflow