std::forwar...assign_range(3)
π NAME π
std::forward_list::assign_range - std::forward_list::assign_range β yes, that's the NAME line, and it's iconic in its redundancy. This is the std::forwar...assign_range(3) page from the C++ Standard Libary manual, bestie. Buckle up. π
β¨ Synopsis β¨
Okay so this one is since C++23 and it's giving template energy:
template< container-compatible-range<T> R > (since C++23)
void assign_range( R&& rg );
What it does: replaces elements in the container with a copy of each element in rg. Like, full replacement, no cap. π
It invalidates all iterators, pointers and references to the elements. So any iterator, pointer or reference to the old elements is cooked. π
Each iterator in the range rg is dereferenced exactly once. One pass, one vibe, no double-dipping. π¦
β οΈ The behavior is undefined if rg overlaps with the container. Not "undefined-ish", not "maybe fine" β undefined. Do not overlap, bestie. π
π Parameters π
rgβ aninput_rangewith reference type convertible to the element type of the container. That's the whole parameter, and she's doing her job. π
πΈ Type requirements πΈ
std::assignable_from<T&, ranges::range_reference_t<R>>must be modeled. Otherwise, the program is ill-formed. Periodt.Tmust beEmplaceConstructibleinto the container from*ranges::begin(rg). Otherwise, the behavior is undefined. So like, respect the requirements or catch these consequences. π
π¦ Return value π¦
(none) β she returns nothing. No notes, no receipts, just vibes. π
β Notes β
- Feature-test macro:
__cpp_lib_containers_rangesβ Value:202202L, Std:(C++23), Feature: Ranges-aware construction and insertion.
π₯ Example π₯
// Run this code
#include <algorithm>
#include <cassert>
#include <forward_list>
#include <list>
int main()
{
const auto source = std::list{2, 7, 1};
auto destination = std::forward_list{3, 1, 4};
#ifdef __cpp_lib_containers_ranges
destination.assign_range(source);
#else
destination.assign(source.cbegin(), source.cend());
#endif
assert(std::ranges::equal(source, destination));
}
π See also π
insert_range_afterβ inserts a range of elements after an element (C++23) (public member function).prepend_rangeβ adds a range of elements to the beginning (C++23) (public member function).assignβ assigns values to the container (public member function).
Alright bestie, that's the page. Source/version and date, folded into a cute sign-off: http://cppreference.com Β· 2024.06.10 Β· std::forwar...assign_range(3). Go forth and assign ranges responsibly. Mwah πβ¨
π¦ 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