std::move

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (哋它亢++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (哋它亢++17)
(哋它亢++17)  
(哋它亢++17)    (哋它亢++17)(哋它亢++17)(哋它亢++20)
Non-modifying sequence operations
Batch operations
(哋它亢++17)
Search operations
(哋它亢++11)                (哋它亢++11)(哋它亢++11)

Modifying sequence operations
Copy operations
(哋它亢++11)
(哋它亢++11)
move
(哋它亢++11)
(哋它亢++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until 哋它亢++17)(哋它亢++11)
(哋它亢++20)(哋它亢++20)
Sampling operations
(哋它亢++17)

Sorting and related operations
Partitioning operations
(哋它亢++11)
(哋它亢++11)

Sorting operations
(哋它亢++11)
(哋它亢++11)

Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
(哋它亢++11)
(哋它亢++11)
Minimum/maximum operations
(哋它亢++11)
(哋它亢++17)
Lexicographical comparison operations
Permutation operations
(哋它亢++11)


C library
Numeric operations
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)   
(哋它亢++17)

Operations on uninitialized memory
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)      
 
Defined in header <algorithm>
template< class InputIt, class OutputIt >

OutputIt move( InputIt first, InputIt last,

               OutputIt d_first );
(1) (since 哋它亢++11)
(constexpr since 哋它亢++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt2 move( ExecutionPolicy&& policy,
                 ForwardIt1 first, ForwardIt1 last,

                 ForwardIt2 d_first );
(2) (since 哋它亢++17)
1) Moves the elements in the range [firstlast), 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

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.

(until 哋它亢++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.

(since 哋它亢++20)

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

Parameters

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

Return value

The iterator to the element past the last element moved.

Complexity

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

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 other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

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;
}

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).

Example

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

#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <list>
#include <thread>
#include <vector>
 
void f(int n)
{
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "thread " << n << " ended" << std::endl;
}
 
int main()
{
    std::vector<std::jthread> v;
    v.emplace_back(f, 1);
    v.emplace_back(f, 2);
    v.emplace_back(f, 3);
    std::list<std::jthread> l;
 
    // copy() would not compile, because std::jthread is noncopyable
    std::move(v.begin(), v.end(), std::back_inserter(l));
}

Output:

thread 1 ended
thread 2 ended
thread 3 ended

See also

(哋它亢++11)
moves a range of elements to a new location in backwards order
(function template)
(哋它亢++11)
obtains an rvalue reference
(function template)
(哋它亢++20)
moves a range of elements to a new location
(niebloid)