std::forwar...assign_range(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 β€” an input_range with 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.
  • T must be EmplaceConstructible into 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