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

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

πŸ’… std::ranges...on_view::end(3) πŸ’…

okay so bestie, this is the C++ Standard Libary page for std::ranges...on_view::end(3) β€” the NAME line goes std::ranges::common_view::end, and no cap, this is the end member of std::ranges::common_view. Let’s get into it 🫢


🌸 NAME 🌸

std::ranges::common_view::end β€” std::ranges::common_view::end (yes, it says it twice; we love a consistent icon πŸ’–)

✨ Synopsis ✨

Two overloads, both since C++20, living their best lives:

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

So like, what are they actually doing?

  1. Returns an iterator representing the end of the common_view, that is:
    • ranges::begin(base_) + ranges::size(base_), if both ranges::random_access_range<V> and ranges::sized_range<V> are satisfied,
    • std::common_iterator<ranges::iterator_t<V>, ranges::sentinel_t<V>>(ranges::end(base_)) otherwise.

Here base_ (the name is for exposition purposes only) is the underlying view.

  1. Same as (1), but V is const-qualified.

πŸ’– Parameters πŸ’–

(none) β€” zero, zip, nada 🌸

πŸ¦‹ Return value πŸ¦‹

An iterator representing the end of the underlying view. That’s the whole vibe πŸ’…

πŸ“ Example πŸ“

Run this code, bestie:

// Run this code

#include <iostream>
#include <numeric>
#include <ranges>

int main()
{
    constexpr int n{4};

    constexpr auto v1 = std::views::iota(1)
                      | std::views::take(n)
                      | std::views::common
                      ;
    constexpr auto v2 = std::views::iota(2)
                      | std::views::take(n)
                      ;
    const int product = std::inner_product(v1.begin(), v1.end(),
                                           v2.begin(),
                                           0);
    std::cout << product << '\n';
}

🌈 Output 🌈

It prints:

40

πŸ’€ Defect reports πŸ’€

The manual says: β€œThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.”

  • LWG 4012 β€” applied to C++20; behavior as published: non-const overload missed simple-view check; correct behavior: added.

πŸ‘‘ See also πŸ‘‘

  • begin (C++20) β€” returns an iterator to the beginning; public member function.
  • ranges::begin (C++20) β€” returns an iterator to the beginning of a range; customization point object.
  • ranges::end (C++20) β€” returns a sentinel indicating the end of a range; customization point object.

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::ranges...on_view::end(3) β€” bye bestie, go crush those ranges πŸ’œβœ¨

πŸ“¦ where this page comes from