std::move - cppreference.com (original) (raw)
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.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
- 1 Parameters
- 2 Return value
- 3 Complexity
- 4 Exceptions
- 5 Possible implementation
- 6 Notes
- 7 Example
- 8 See also
[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:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is one of the standard policies, std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined. - If the algorithm fails to allocate memory, std::bad_alloc is thrown.
[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] |