Przejdź do głównej zawartości

std::move() algorithm

// (1)
template< class InputIt, class OutputIt >
constexpr OutputIt move( InputIt first, InputIt last, OutputIt d_first );

// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 move( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );
  • (1) Moves the elements in the range [first; last), to another range beginning at d_first, starting from first and proceeding to last - 1.

    warning

    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.

Overload Resolution

These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to move.

d_first

The beginning of the destination range.

Undefined Behaviour

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

policy

The execution policy to use. See execution policy for details.

Type requirements

InputItLegacyInputIterator
OutputItLegacyOutputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator

Return value

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

Complexity

Exactly last - first assignments.

Exceptions

The overloads with a template parameter named ExecutionPolicy report 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 none other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

move (1)
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).

Examples

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

Main.cpp
#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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::move() algorithm

// (1)
template< class InputIt, class OutputIt >
constexpr OutputIt move( InputIt first, InputIt last, OutputIt d_first );

// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 move( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );
  • (1) Moves the elements in the range [first; last), to another range beginning at d_first, starting from first and proceeding to last - 1.

    warning

    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.

Overload Resolution

These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to move.

d_first

The beginning of the destination range.

Undefined Behaviour

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

policy

The execution policy to use. See execution policy for details.

Type requirements

InputItLegacyInputIterator
OutputItLegacyOutputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator

Return value

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

Complexity

Exactly last - first assignments.

Exceptions

The overloads with a template parameter named ExecutionPolicy report 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 none other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

move (1)
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).

Examples

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

Main.cpp
#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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.