std::ranges...w::drop_view(3)

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

πŸ’… NAME πŸ’…

ok so this is the std::ranges::drop_view::drop_view page from the C++ Standard Libary manual, section 3 β€” yes, literally just the constructors of drop_view, and yes they're iconic. It's the "make a view that skips the first count things" moment, and we're gonna walk through it together, bestie 🫢


✨ SYNOPSIS ✨

Here's the whole vibe, exactly as printed, no cap:

     drop_view() requires std::default_initializable<V> = default;
  (1) (since C++20)
     constexpr explicit drop_view( V base,
  (2) (since C++20)
     ranges::range_difference_t<V> count );

🌸 DESCRIPTION 🌸

So basically: this constructor constructs a drop_view. That's it. That's the whole thing. But let me break down the two flavors, because they hit different πŸ’–

  1. The default constructor. Zero-arg, no thoughts, head empty β€” it value-initializes the underlying view and initializes the count to 0. After construction, base() returns a copy of V() and size() equals the size of the underlying view. So nothing gets dropped, it's giving "I woke up and chose peace" energy 🧚
  2. The two-parameter constructor. This one's the main character: it initializes the underlying view with std::move(base) and the count with count. After construction, base() returns a copy of base, and size() returns ranges::size(base) - count if the size of base is not less than count, or 0 otherwise. So if you ask it to skip more than it's got... it just gives you 0. Graceful. Demure. Mindful πŸ’…

πŸŽ€ PARAMETERS πŸŽ€

  • base β€” the underlying view, the source material, the origin story ✨
  • count β€” number of elements to skip. Like scrolling past the first few posts fr fr

πŸ”₯ EXAMPLE πŸ”₯

The page says "// Run this code", so run it, bestie:

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

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

            std::ranges::for_each(hi, [](const char c){ std::cout << c; });
            std::cout << '\n';

            constexpr auto n = std::distance(hi.cbegin(), std::ranges::find(hi,
       'C'));

            auto cxx = std::ranges::drop_view{hi, n};

            std::ranges::for_each(cxx, [](const char c){ std::cout << c; });
            std::cout << '\n';
        }

🌈 Output:

        Hello, C++20
        C++20

look at that. it dropped everything before the C and the result is literally C++20. ate and left no crumbs πŸ‘‘


πŸ’€ DEFECT REPORTS πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards β€” aka the committee went back and fixed the past, which is such a flex:

  • LWG 3714 β€” applied to C++20. Behavior as published: the multi-parameter constructor was not made explicit. Correct behavior: it was made explicit (that's the (P2711R1) note next to it). So yeah, it's explicit now, periodt.

ngl, the one thing to remember: constructor (1) is defaulted but requires std::default_initializable<V>, and constructor (2) is constexpr explicit. Don't come for me if you forget the explicit β€” the DR already handled that for you 😭

okay that's the whole page, every flag and flagless detail intact, I promise 🫢 this was brought to you by http://cppreference.com, version 2024.06.10 β€” mwah, go drop some views bestie πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from