class template
<iterator>

std::move_iterator

template <class Iterator> class move_iterator;
Move iterator
This class adapts an iterator so that dereferencing it produces rvalue references (as if std::move was applied), while all other operations behave as in the regular iterator.

This iterator adaptor keeps an internal copy of an iterator (known as its base iterator) on which all operations are reflected. A copy of the base iterator with the current state can be obtained at any time by calling member base.

Template parameters

Iterator
An input iterator type.
The use of certain of its members may require the iterator category to be bidirectional or random-access.

Member types

memberdefinition in move_iteratordescription
iterator_typeIteratorIterator's type
iterator_categoryiterator_traits<Iterator>::iterator_categoryPreserves Iterator's category
value_typeiterator_traits<Iterator>::value_typePreserves Iterator's value type
difference_typeiterator_traits<Iterator>::difference_typePreserves Iterator's difference type
pointerIterator(same as iterator_type)
referencevalue_type&&

Member functions


Non-member function overloads


Non-member helper functions


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// move_iterator example
#include <iostream>     // std::cout
#include <iterator>     // std::move_iterator
#include <vector>       // std::vector
#include <string>       // std::string
#include <algorithm>    // std::copy

int main () {
  std::vector<std::string> foo (3);
  std::vector<std::string> bar {"one","two","three"};

  typedef std::vector<std::string>::iterator Iter;

  std::copy ( std::move_iterator<Iter>(bar.begin()),
              std::move_iterator<Iter>(bar.end()),
              foo.begin() );

  // bar now contains unspecified values; clear it:
  bar.clear();

  std::cout << "foo:";
  for (std::string& x : foo) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Output:

foo: one two three


Possible implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace std {
template <class Iterator> class move_iterator {
  Iterator current;
public:
  typedef Iterator iterator_type;
  typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
  typedef typename iterator_traits<Iterator>::value_type        value_type;
  typedef typename iterator_traits<Iterator>::difference_type   difference_type;
  typedef Iterator pointer
  typedef value_type&& reference;

  move_iterator() {}
  explicit move_iterator (Iterator it) : current(it) {}
  template <class Iter> move_iterator (const move_iterator<Iter>& it) : current(it.current) {}
  template <class Iter> move_iterator& operator= (const move_iterator<Iter>& it)
  { current = it.current; }

  iterator_type base() const { return current; }
  pointer operator->() const { return current; }
  reference operator*() const { return std::move(*current); }
  move_iterator& operator++() { ++current; return *this; }
  move_iterator& operator--() { --current; return *this; }
  move_iterator& operator++(int) { move_iterator temp = *this; ++current; return temp; }
  move_iterator& operator--(int) { move_iterator temp = *this; --current; return temp; }
  move_iterator operator+ (difference_type n) const { return move_iterator(current+n); }
  move_iterator operator- (difference_type n) const { return move_iterator(current-n); }
  move_iterator operator+= (difference_type n) { current+=n; return *this; }
  move_iterator operator-= (difference_type n) { current-=n; return *this; }
  auto operator[](difference_type n) const -> decltype(std::move(current[n]))
  { return std::move(current[n]); }
};
}

See also