std::ranges...move_result(3)
β¨ std::ranges::move, std::ranges::move_result β¨
Okay bestie, welcome to the C++ Standard Library page for std::ranges...move_result(3) β yes, the title is a whole vibe, and yes, we're about to understand it. π«Ά
πΈ NAME πΈ
std::ranges::move,std::ranges::move_result β std::ranges::move,std::ranges::move_result, exactly as the page says. No cap.
π¦ Synopsis π¦
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>
(1) (since C++20)
constexpr move_result<I, O>
move( I first, S last, O result );
template< ranges::input_range R, std::weakly_incrementable O >
requires std::indirectly_movable<ranges::iterator_t<R>, O>
(2) (since C++20)
constexpr move_result<ranges::borrowed_iterator_t<R>, O>
move( R&& r, O result );
π§Έ Helper types π§Έ
template< class I, class O >
(3) (since C++20)
using move_result = ranges::in_out_result<I, O>;
- Moves the elements in the range, defined by
[first, last), to another range beginning atresult. The behavior is undefined ifresultis within the range[first, last). In such a case,ranges::move_backwardmay be used instead. π¨ - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirst, andranges::end(r)aslast.
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. Not N+1, not a mystery number β exactly N. β¨
π 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). Overlap tea, served correctly. π΅
π§ 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 {};
π Example π
The following code moves thread objects (which themselves are non copyable) from one container to another.
// Run this code
#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 π
ranges::move_backwardβ moves a range of elements to a new location in backwards order.(C++20)(niebloid)ranges::copy,ranges::copy_ifβ copies a range of elements to a new location.(C++20)(niebloid)ranges::copy_backwardβ copies a range of elements in backwards order.(C++20)(niebloid)moveβ moves a range of elements to a new location.(C++11)(function template)moveβ obtains an rvalue reference.(C++11)(function template)
Source http://cppreference.com, version/date 2024.06.10, page std::ranges...move_result(3) β bye bestie, go move those elements with main character energy! πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms