On this page
std::ranges::move, std::ranges::move_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Helper types | ||
|
(3) | (since C++20) |
[
first
,
last
)
, to another range beginning at result
. The behavior is undefined if result
is within the range [
first
,
last
)
. In such a case, ranges::move_backward
may be used instead.
r
as the source range, as if using ranges::begin(r)
as first
, and ranges::end(r)
as last
.
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.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
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 |
Return value
{last, result + N}
, where
N = ranges::distance(first, last)
.
N = ranges::distance(r)
.
Complexity
Exactly N
move assignments.
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).
Possible implementation
|
Example
The following code moves thread objects (which themselves are non copyable) from one container to another.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <list>
#include <thread>
#include <vector>
using namespace std::literals::chrono_literals;
void f(std::chrono::milliseconds n)
{
std::this_thread::sleep_for(n);
std::cout << "thread with n=" << n.count() << "ms ended" << std::endl;
}
int main()
{
std::vector<std::jthread> v;
v.emplace_back(f, 400ms);
v.emplace_back(f, 600ms);
v.emplace_back(f, 800ms);
std::list<std::jthread> l;
// std::ranges::copy() would not compile, because std::jthread is non-copyable
std::ranges::move(v, std::back_inserter(l));
}
Output:
thread with n=400ms ended
thread with n=600ms ended
thread with n=800ms ended
See also
(C++20)
|
moves a range of elements to a new location in backwards order (niebloid) |
(C++20)(C++20)
|
copies a range of elements to a new location (niebloid) |
(C++20)
|
copies a range of elements in backwards order (niebloid) |
(C++11)
|
moves a range of elements to a new location (function template) |
(C++11)
|
obtains an rvalue reference (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/move