Namespaces
Variants
Views
Actions

std::ranges::move, std::ranges::move_result

From cppreference.com
< cpp‎ | algorithm‎ | ranges
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms: std::ranges::copy, std::ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
C library
 
Constrained algorithms
Non-modifying sequence operations
Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutations
 
Defined in header <algorithm>
Call signature
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>

  requires std::indirectly_movable<I, O>
    constexpr ranges::move_result<I, O>

      ranges::move( I first, S last, O result );
(1) (since C++20)
template<ranges::input_range R, std::weakly_incrementable O>

  requires std::indirectly_movable<ranges::iterator_t<R>, O>
    constexpr ranges::move_result<ranges::borrowed_iterator_t<R>, O>

      ranges::move( R&& r, O result );
(2) (since C++20)
Helper types
template<class I, class O>
  using move_result = ranges::in_out_result<I, O>;
(3) (since C++20)
1) Moves the elements in the range, defined by [first, last), to another range beginning at result.

Precondition: result is not within (first, last], otherwise the behavior is undefined and std::ranges::move_backward should be used instead.

Note: 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 uses r as the source range, as if using ranges::begin(r) as first, and ranges::end(r) as last.

The function-like entities described on this page are niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

first - the beginning of the range of elements to move
last - the end of the range of elements to move
r - the range of the elements to move
result - the beginning of the destination range

[edit] Return value

1) The ranges::move_result<I, O>{last, result + N}, where N = last - first.
2) Same as (1), but return type is ranges::move_result<ranges::borrowed_iterator_t<R>, I>.

[edit] Complexity

1) Exactly last - first move assignments.
2) Exactly std::ranges::size(r) move assignments.

[edit] Notes

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

[edit] Possible implementation

struct move_fn {
  template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_movable<I, O>
      constexpr ranges::move_result<I, O>
        operator()( I first, S last, O result ) const {
          for (; first != last; ++first, ++result)
            *result = ranges::iter_move(first);
          return {std::move(first), std::move(result)};
        }
  template<ranges::input_range R, std::weakly_incrementable O>
    requires std::indirectly_movable<ranges::iterator_t<R>, O>
      constexpr ranges::move_result<ranges::borrowed_iterator_t<R>, O>
        operator()( R&& r, O result ) const {
          return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
        }
};
 
inline constexpr move_fn move{};

[edit] Example

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

#include <algorithm>
#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" << 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;
 
    // ranges::copy() would not compile, because std::jthread is noncopyable
    std::ranges::move(v, std::back_inserter(l));
}

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
(niebloid) [edit]
copies a range of elements to a new location
(niebloid) [edit]
copies a range of elements in backwards order
(niebloid) [edit]
(C++11)
moves a range of elements to a new location
(function template) [edit]
(C++11)
obtains an rvalue reference
(function template) [edit]