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

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

okay bestie, this is the std::ranges..._view::size(3) page from the C++ Standard Library πŸ’… and we’re gonna make drop_view::size make sense, fr fr.

πŸ’– NAME πŸ’–

std::ranges::drop_view::size β€” std::ranges::drop_view::size

Like… it tells you how many elements are left after the drop. iconic behavior.


✨ Synopsis ✨

          constexpr auto size() requires ranges::sized_range<V>;
       (1) (since C++20)
          constexpr auto size() const requires ranges::sized_range<const V>;
       (2) (since C++20)

So you got two overloads, both constexpr, both since C++20:

  • (1) β€” constexpr auto size() requires ranges::sized_range<V>;
  • (2) β€” constexpr auto size() const requires ranges::sized_range<const V>;

They return the number of elements. That’s the whole vibe πŸŽ€

Let base_ be the underlying view, and let count_ be the stored count β€” usually the number passed to the constructor, or 0 if *this is default constructed. It’s equivalent to:

const auto s = ranges::size(base_);
const auto c = static_cast<decltype(s)>(count_);
return s < c ? 0 : s - c;

The way that little s < c ? 0 : s - c handles the β€œdrop more than we have” situation? lowkey elegant πŸ’€βœ¨


🫢 Parameters 🫢

(none) β€” no parameters, bestie. Not a single one. 🫢


πŸŽ€ Return value πŸŽ€

The number of elements. That’s it. That’s the return value. 🌸


🧚 Example 🧚

// Run this code

#include <array>
#include <ranges>

int main()
{
    constexpr std::array a{42, 43, 44};
    static_assert(std::ranges::drop_view{std::views::all(a), 0}.size() == 3);
    static_assert(std::ranges::drop_view{std::views::all(a), 1}.size() == 2);
    static_assert(std::ranges::drop_view{std::views::all(a), 2}.size() == 1);
    static_assert(std::ranges::drop_view{std::views::all(a), 3}.size() == 0);
    static_assert(std::ranges::drop_view{std::views::all(a), 4}.size() == 0);
}

Not the static_asserts proving every drop count from 0 to 4? Obsessed. The 4 case still gives 0, not negative, because the ternary said β€œbestie, no” πŸ˜­πŸ’…


🌈 See also 🌈

  • ranges::size β€” returns an integer equal to the size of a range; it’s a customization point object since C++20.
  • ranges::ssize β€” returns a signed integer equal to the size of a range; it’s also a customization point object since C++20.

source: http://cppreference.com 🫢 dated 2024.06.10 ✨ periodt, go count your elements πŸ’œ

πŸ“¦ where this page comes from