The dereference prefix operator * being applied to iterator returns the lvalue reference to the element pointed by the iterator. Therefore, algorithms such as std::copy or std::transform calls copying constructors of processed elements, for example:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <algorithm> #include <list> #include <string> #include <vector> auto deep_copy_to_list(const std::vector<std::string>& src) { std::list<std::string> dst(src.size()); // allocate the memory to store src.size() elements copy(cbegin(src), cend(src), begin(dst)); // copy the strings return dst; } |
However it’s possible to force algorithms to call moving constructors when it’s appropriate for performance reasons by using the special adapter called std::move_iterator provided by the standard library. This adapter class defines the dereference operator * in such a way that it returns the rvalue (rather than lvalue) reference to the element pointer by the iterator. The object of type std::move_iterator can be conveniently created by the function std::make_move_iterator that deduces the underlying type of the iterator object from it’s argument automatically, for example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <algorithm> #include <list> #include <string> #include <vector> auto cheap_move_to_list(std::vector<std::string>&& src) { std::list<std::string> dst(src.size()); // allocate the memory copy(make_move_iterator(begin(src)), make_move_iterator(end(src)), begin(dst)); // move the strings return dst; } |



