std::move - cppreference.com (original) (raw)

Defined in header
template< class InputIt, class OutputIt > OutputIt move( InputIt first, InputIt last, OutputIt d_first ); (1) (since C++11) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 move( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first ); (2) (since C++17)
  1. Moves the elements in the range [first, last), to another range beginning at d_first, starting from first and proceeding to last. After this operation the elements in the moved-from range will still contain valid values of the appropriate type, but not necessarily the same values as before the move.

  2. Same as (1), but executed according to policy.

This overload participates in overload resolution only if all following conditions are satisfied:

If d_first is within the range [first, last), the behavior is undefined. In this case, std::move_backward may be used instead.

Contents

[edit] Parameters

first, last - the pair of iterators defining the source range of elements to move
d_first - the beginning of the destination range
policy - the execution policy to use
Type requirements
-InputIt must meet the requirements of LegacyInputIterator.
-OutputIt must meet the requirements of LegacyOutputIterator.
-ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.

[edit] Return value

The iterator to the element past the last element moved.

[edit] Complexity

Exactly std::distance(first, last) move assignments.

[edit] Exceptions

The overload with a template parameter named ExecutionPolicy reports errors as follows:

[edit] Possible implementation

template<class InputIt, class OutputIt> OutputIt move(InputIt first, InputIt last, OutputIt d_first) { for (; first != last; ++d_first, ++first) *d_first = std::move(*first);   return d_first; }

[edit] Notes

When moving overlapping ranges, std::move is appropriate when moving to the left (beginning of the destination range is outside the source range) while std::move_backward is appropriate when moving to the right (end of the destination range is outside the source range).

[edit] Example

The following code moves thread objects (which themselves are not copyable) from one container to another.

Output:

thread 1 ended thread 2 ended thread 3 ended

[edit] See also

| | moves a range of elements to a new location in backwards order (function template) [edit] | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | | converts the argument to an xvalue (function template) [edit] | | | moves a range of elements to a new location(algorithm function object)[edit] |