std::ranges...ke_view::end(3)

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

πŸ’… std::ranges::take_view::end πŸ’…

manual vibe: C++ Standard Libary, section 3 β€” yes the header really spells it like that, and we're keeping the lore. this page is the C++20 take_view::end situation, bestie.

🌸 NAME 🌸

std::ranges::take_view::end β€” okay so this is literally the end() member of std::ranges::take_view, and the NAME line is just the same thing twice like a little echo. iconic.

✨ SYNOPSIS ✨

constexpr auto end() requires (!__SimpleView<V>);           (1)
(since C++20)
constexpr auto end() const requires ranges::range<const V>; (2)
(since C++20)

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so end() gives you back a sentinel or an iterator that represents the end of the take_view. The end of the take_view is either one past the count-th element in the underlying range, or the end of the underlying range if the latter has less than count elements. no cap.

  1. Returns a take_view::/*sentinel*/<false>, a std::default_sentinel_t, or a ranges::iterator_t<V>.
  2. Returns a take_view::/*sentinel*/<true>, a std::default_sentinel_t, or a ranges::iterator_t<const V>.
cute caveat: overload (1) does not participate in overload resolution if V is a simple view β€” that is, if V and const V are views with the same iterator and sentinel types. periodt.

🫢 PARAMETERS 🫢

(none) β€” literally no parameters, bestie. zero. the void is cute.

πŸ”₯ RETURN VALUE πŸ”₯

The result depends on the concepts satisfied by the possibly const-qualified underlying view type Base_, that is V (for overload (1)) or const V (for overload (2)).

Let base_ be the underlying view.

  • If the underlying view satisfies sized_range and random_access_range: it returns ranges::begin(base_) + ranges::range_difference_t<Base_>(this->size()).
  • If the underlying view satisfies sized_range but not random_access_range: it returns std::default_sentinel.
  • If the underlying view does not satisfy sized_range: overload (1) returns 1) /*sentinel*/<false>{ranges::end(base_)}, and overload (2) returns 2) /*sentinel*/<true>{ranges::end(base_)}.
iykyk: that's the whole table from the page, just with better vibes and every cell kept intact πŸ’–

πŸŽ€ EXAMPLE πŸŽ€

// Run this code

#include <iostream>
#include <iterator>
#include <list>
#include <ranges>
#include <type_traits>

int main()
{
    const auto list1 = {3, 1, 4, 1, 5};
    const auto seq1 = list1 | std::views::take(4);
    static_assert(std::ranges::sized_range<decltype(seq1)> &&
                  std::ranges::random_access_range<decltype(seq1)> &&
                  std::is_same_v<decltype(seq1.end()),
       decltype(list1.end())>);
    for (auto it = seq1.begin(); it != seq1.end(); ++it)
        std::cout << *it << ' ';
    std::cout << '\n';

    std::list list2 = {2, 7, 1, 8, 2};
    const auto seq2 = list2 | std::views::take(4);
    static_assert(std::ranges::sized_range<decltype(seq2)> &&
                  not std::ranges::random_access_range<decltype(seq2)>
       &&
                  std::is_same_v<decltype(seq2.end()),
       std::default_sentinel_t>);
    for (auto it = seq2.begin(); it != std::default_sentinel; ++it)
        std::cout << *it << ' ';
    std::cout << '\n';
}

🌈 OUTPUT 🌈

3 1 4 1
2 7 1 8

πŸ’€ DEFECT REPORTS πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • P2393R1 β€” applied to C++20; behavior as published: implicit conversions between signed and unsigned integer-class types might fail; correct behavior: made explicit.

πŸ‘‘ SEE ALSO πŸ‘‘

  • begin (C++20) β€” returns an iterator to the beginning (public member function)
  • counted_iterator (C++20) β€” iterator adaptor that tracks the distance to the end of the range (class template)
  • operator== (C++20) β€” compares a sentinel with an iterator returned from take_view::begin (function)

okay bye bestie πŸ’– this page came from http://cppreference.com, version/date 2024.06.10, and yes that's the footer energy folded into a cute little sign-off. mwah πŸ’‹

πŸ“¦ where this page comes from