std::move - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::move

From cppreference.com
< cpp‎ | algorithm

Defined in header `<algorithm>` |  |
template< class InputIt, class OutputIt > OutputIt move( InputIt first, InputIt last, OutputIt d_first ); | (1) | (since C++11)
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`. 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 only participates in overload resolution if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

### [edit] Parameters

first, last | - | the range of elements to move
d_first | - | the beginning of the destination range. The behavior is undefined if `d_first` is within the range `[first, last)`. In this case, std::move_backward may be used instead of std::move.
policy | - | the execution policy to use. See execution policy for details.
Type requirements
- `InputIt` must meet the requirements of `InputIterator`.
- `OutputIt` must meet the requirements of `OutputIterator`.
- `ForwardIt1, ForwardIt2` must meet the requirements of `ForwardIterator`.

### [edit] Return value

Output iterator to the element past the last element moved (d_first + (last - first))

### [edit] Complexity

Exactly `last - first` 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 three 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.

### [edit] Possible implementation

    template<class InputIt, class OutputIt>
    OutputIt move(InputIt first, InputIt last, OutputIt d_first)
    {
        while (first != last) {
            *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.

Run this code

    #include <iostream>
    #include <vector>
    #include <list>
    #include <iterator>
    #include <thread>
    #include <chrono>

    void f(int n)
    {
        std::this_thread::sleep_for(std::chrono::seconds(n));
        std::cout << "thread " << n << " ended" << '\n';
    }

    int main()
    {
        std::vector<std::thread> v;
        v.emplace_back(f, 1);
        v.emplace_back(f, 2);
        v.emplace_back(f, 3);
        std::list<std::thread> l;
        // copy() would not compile, because std::thread is noncopyable

        std::move(v.begin(), v.end(), std::back_inserter(l));
        for (auto& t : l) t.join();
    }

Output:

    thread 1 ended
    thread 2 ended
    thread 3 ended

### [edit] See also

move_backward (C++11) | moves a range of elements to a new location in backwards order (function template) [edit]
move (C++11) | obtains an rvalue reference (function template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/move&oldid=90633"