std::ranges...p_view::size(3)

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

πŸ’… std::ranges::zip_view::size(3) πŸ’…

okay bestie, welcome to the std::ranges::zip_view::size(3) page from the C++ Standard Library β€” manual name folded in near the top like a little name tag ✨. The header up there is giving std::ranges...p_view::size(3) and the manual name is spelled C++ Standard Libary in the original (not me, I promise 😭), but the full NAME is std::ranges::zip_view::size.


🌸 NAME 🌸

std::ranges::zip_view::size β€” literally std::ranges::zip_view::size, no cap. This is the member that tells you how many elements are in your zip_view πŸ’–.

✨ Synopsis ✨

          constexpr auto size()                                   (1) (since
       C++23)
              requires (ranges::sized_range<Views> && ...);
          constexpr auto size() const                             (2) (since
       C++23)
              requires (ranges::sized_range<const Views> && ...);

It returns the number of elements in the zip_view 🫢. It’s provided only if each underlying (adapted) range satisfies sized_range.

Equivalent to:

        return std::apply
        (
            [](auto... sizes)
            {
                using CT = /*make-unsigned-like-
       t*/<std::common_type_t<decltype(sizes)...>>;
                return ranges::min({CT(sizes)...});
            },
            /*tuple-transform*/(ranges::size, views_)
        );

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally none, zero, zilch. It takes no parameters, bestie 🫢.

🌈 Return value 🌈

The number of elements β€” which is the minimum size among all sizes of adapted views. So it’s like the shortest friend in the group chat deciding how many people fit in the pic 😭✨.

πŸ“ Example πŸ“

        // Run this code

        #include <algorithm>
        #include <cassert>
        #include <deque>
        #include <forward_list>
        #include <ranges>
        #include <vector>

        int main()
        {
            auto x = std::vector{1, 2, 3, 4, 5};
            auto y = std::deque{'a', 'b', 'c'};
            auto z = std::forward_list{1., 2.};

            auto v1 = std::views::zip(x, y);
            assert(v1.size() == std::min(x.size(), y.size()));
            assert(v1.size() == 3);

            [[maybe_unused]] auto v2 = std::views::zip(x, z);
        //  auto sz = v2.size(); // Error, v2 does not have size():
            static_assert(not std::ranges::sized_range<decltype(z)>);
        }

That commented-out line is the whole vibe: v2.size() would be an error because v2 does not have size() πŸ’€, and the static_assert checks that decltype(z) is not a std::ranges::sized_range.

πŸ¦‹ See also πŸ¦‹

  • ranges::size β€” returns an integer equal to the size of a range (C++20) (customization point object) ✨
  • ranges::ssize β€” returns a signed integer equal to the size of a range (C++20) (customization point object) 🫢

mwah πŸ’‹ β€” cute little sign-off for the footer: source/version and date are http://cppreference.com, 2024.06.10, page std::ranges...p_view::size(3). That’s the footer folded into a sign-off instead of copied as a line πŸŽ€.

πŸ“¦ where this page comes from