std::ranges..._view::begin(3)

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

✨ std::ranges::drop_view::begin ✨

okay so this is the std::ranges..._view::begin(3) page from the C++ Standard Libary manual, bestie. we are talking about std::ranges::drop_view::begin, and yes, it is exactly as niche and iconic as it sounds. πŸ’…

🌸 NAME 🌸

std::ranges::drop_view::begin β€” std::ranges::drop_view::begin. periodt. 🫢

πŸ¦‹ Synopsis πŸ¦‹

two overloads, both constexpr auto begin(), both since C++20, and the requires clauses are doing main character energy:

constexpr auto begin()

    requires (!(__SimpleView<V> &&                       (1) (since C++20)
                 ranges::random_access_range<const V> &&

                 ranges::sized_range<const V>));
constexpr auto begin() const

    requires ranges::random_access_range<const V> &&     (2) (since C++20)

             ranges::sized_range<const V>;

what it does: returns an iterator to the first element of the drop_view, that is, an iterator to the N^th element of the underlying view, or to the end of the underlying view if it has less than N elements. πŸ’–

important vibe: if V is not a random_access_range or a sized_range, in order to provide the amortized constant time complexity required by the range concept, the overload (1) caches the result within the drop_view object for use on subsequent calls. no cap, that cache is doing the work. ✨

πŸŽ€ Parameters πŸŽ€

(none) β€” literally no parameters, bestie. zero. zilch. nada. 🫢

πŸ’œ Return value πŸ’œ

ranges::next(ranges::begin(base_), count_, ranges::end(base_)), where base_ is the underlying view, and count_ is the number of elements to skip. that's the whole return value, and yes it's giving iterator math. πŸ”₯

πŸ¦‹ Example πŸ¦‹

here's the example, copied exactly because we do not mess with code. run it if you want; the comment says // Run this code and we respect it. πŸ’»

       // Run this code

        #include <array>
        #include <concepts>
        #include <iostream>
        #include <iterator>
        #include <ranges>

        void println(std::ranges::range auto const& range)
        {
            for (auto const& elem : range)
                std::cout << elem;
            std::cout << '\n';
        }

        int main()
        {
            std::array hi{'H', 'e', 'l', 'l', 'o', ',', ' ', 'C', '+', '+',
       '2', '0', '!'};
            println(hi);

            const auto pos = std::distance(hi.begin(), std::ranges::find(hi,
       'C'));
            auto cxx = std::ranges::drop_view{hi, pos};
            std::cout << "*drop_view::begin() == '" << *cxx.begin() << "'\n";
        //  *cxx.begin() = 'c'; // undefined: 'views' are to be used as observers
            println(cxx);
        }

πŸ“ Output: πŸ“

Hello, C++20!
*drop_view::begin() == 'C'
C++20!

πŸ‘‘ Defect reports πŸ‘‘

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. yes, they went back and fixed the lore. 😭

  • LWG 3482 β€” applied to C++20. Behavior as published: the const overload can be called with unsized ranges. Correct behavior: the const overload requires sized_range.

🌈 See also 🌈

other pages in this cinematic universe: πŸ‘‘

  • end (C++20) β€” returns an iterator or a sentinel to the end (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)

kisses, source/version http://cppreference.com, date 2024.06.10, page std::ranges..._view::begin(3) πŸ’–

πŸ“¦ where this page comes from