std::move_iterator - cppreference.com

From cppreference.com

template<classIter>classmove_iterator;(since C++11)std::move_iterator is an iterator adaptor which behaves exactly like the underlying iterator (which must be at least a

LegacyInputIterator

or model

input_iterator

(since C++20), or stronger iterator concept(since C++23)), except that dereferencing converts the value returned by the underlying iterator into an rvalue. If this iterator is used as an input iterator, the effect is that the values are moved from, rather than copied from.

Nested types

Type Definition iterator_typeIteriterator_categorystd::iterator_traits<Iter>::iterator_categoryvalue_typestd::iterator_traits<Iter>::value_typedifference_typestd::iterator_traits<Iter>::difference_typepointerIterreferencethe rvalue reference version of std::iterator_traits<Iter>::reference if std::iterator_traits<Iter>::reference is a reference type

std::iterator_traits<Iter>::reference otherwise

(until C++20)Type Definition iterator_typeIteriterator_category
(conditionally present)undefined if std::iterator_traits<Iter>::iterator_category is invalid or does not denote a type

std::random_access_iterator_tag

if std::iterator_traits<Iter>::iterator_category models std::derived_from<std::random_access_iterator_tag>

std::iterator_traits<Iter>::iterator_category otherwise

iterator_conceptvalue_typestd::iter_value_t<Iter>difference_typestd::iter_difference_t<Iter>pointerIterreferencestd::iter_rvalue_reference_t<Iter>(since C++20)Data members

Member Description Itercurrentthe underlying iterator
(exposition-only member object*)Member functions

(constructor)

constructs a new move_iterator
(public member function)

[edit]

operator=

assigns another move_iterator
(public member function)

[edit]

base

accesses the underlying iterator
(public member function)

[edit]

operator*operator->

accesses the pointed-to element
(public member function)

[edit]

operator[]

accesses an element by index
(public member function)

[edit]

operator++operator++(int)operator+=operator+operator--operator--(int)operator-=operator-

advances or decrements the move_iterator
(public member function)

[edit]

Non-member functions

operator==operator!=operator<operator<=operator>operator>=operator<=>

(C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)

compares the underlying iterators
(function template)

[edit]

operator==(std::move_sentinel)

(C++20)

compares the underlying iterator and the underlying sentinel
(function template)

[edit]

operator+

(C++11)

advances the iterator
(function template)

[edit]

operator-

(C++11)

computes the distance between two iterator adaptors
(function template)

[edit]

operator-(std::move_sentinel)

(C++20)

computes the distance between the underlying iterator and the underlying sentinel
(function template)

[edit]

iter_move

(C++20)

casts the result of dereferencing the underlying iterator to its associated rvalue reference type
(function)

[edit]

iter_swap

(C++20)

swaps the objects pointed to by two underlying iterators
(function template)

[edit]

make_move_iterator

(C++11)

creates a

std::move_iterator

of type inferred from the argument
(function template)

[edit]

Helper templates

template<classIterator1,classIterator2>requires(!std::sized_sentinel_for<Iterator1,Iterator2>)constexprbooldisable_sized_sentinel_for<std::move_iterator<Iterator1>,std::move_iterator<Iterator2>>=true;(since C++20)This partial specialization of std::disable_sized_sentinel_for prevents specializations of move_iterator from satisfying

sized_sentinel_for

if their underlying iterators do not satisfy the concept.

Notes

Feature-test

macroValueStdFeature

__cpp_lib_move_iterator_concept

202207L

(C++23)Make std::move_iterator<T*> a random access iterator Example

Run this code

#include<algorithm>#include<iomanip>#include<iostream>#include<iterator>#include<ranges>#include<string>#include<string_view>#include<vector>voidprint(conststd::string_viewrem,constauto&v){std::cout<<rem;for(constauto&s:v)std::cout<<std::quoted(s)<<' ';std::cout<<'\n';};intmain(){std::vector<std::string>v{"this","_","is","_","an","_","example"};print("Old contents of the vector: ",v);std::stringconcat;for(autobegin=std::make_move_iterator(v.begin()),end=std::make_move_iterator(v.end());begin!=end;++begin){std::stringtemp{*begin};// moves the contents of *begin to tempconcat+=temp;}// Starting from C++17, which introduced class template argument deduction,// the constructor of std::move_iterator can be used directly:// std::string concat = std::accumulate(std::move_iterator(v.begin()),// std::move_iterator(v.end()),// std::string());print("New contents of the vector: ",v);print("Concatenated as string: ",std::ranges::single_view(concat));}Possible output:

Old contents of the vector: "this" "_" "is" "_" "an" "_" "example" New contents of the vector: "" "" "" "" "" "" "" Concatenated as string: "this_is_an_example" Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 2106

C++11 dereferencing a move_iterator could return a dangling reference
if the dereferencing the underlying iterator returns a prvalue returns the
object instead

LWG 3736

C++20 move_iterator was missing disable_sized_sentinel_for specialization added

P2259R1

C++20 member iterator_category was defined even if
std::iterator_traits<Iter>::iterator_category is not defined iterator_category is
not defined in this case See also