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

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

πŸ’… std::ranges..._view::size(3) β€” C++ Standard Libary energy ✨

Hey bestie, welcome to the C++ Standard Libary page for std::ranges..._view::size(3). No cap, this is the size member of std::ranges::adjacent_view, and she's here to tell you how many elements are in the house. 🫢

πŸ’… NAME πŸ’…

std::ranges::adjacent_view::size β€” std::ranges::adjacent_view::size. That's the name, bestie, it's giving β€œI repeat myself because that's literally the manpage NAME line.” πŸ’€

✨ Synopsis ✨

Two overloads, both (since C++23), both constexpr, and both gated by a requires clause. Here's the exact signature tea:

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

Returns the number of elements. Let base_ be the underlying view. Equivalent to:

using SizeType = decltype(ranges::size(base_));
using CommonType = ranges::common_type_t<SizeType, std::size_t>;
auto size = static_cast<CommonType>(ranges::size(base_));
size -= std::min<CommonType>(size, N - 1);
return static_cast<SizeType>(size);

πŸŽ€ Parameters πŸŽ€

(none) β€” she takes zero parameters. Nothing. Nada. Iconic minimalism. 🧚

πŸ”₯ Return value πŸ”₯

The number of elements, may be 0 if ranges::size(base_) is less than N. So if the underlying range is too short, bestie can absolutely hand you a zero. No drama, just math. ✨

🌈 Example 🌈

The page says // Run this code, and here's the exact code, no edits, no cap:

#include <ranges>

int main()
{
    constexpr static auto v = {1, 2, 3, 4, 5, 6};

    constexpr int width1 {4};
    constexpr auto view1 {std::views::adjacent<width1>(v)};
    static_assert(view1.size() == 3);
    static_assert(view1.size() == (v.size() - width1 + 1));

    constexpr int width2 {8};
    constexpr auto view2 {std::views::adjacent<width2>(v)};
    // window is too wide, so view2 has no elements:
    static_assert(view2.size() == 0);
}

πŸ¦‹ 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).

That's the whole page, bestie β€” slay, periodt, and may your size() always be sized. πŸ’–

xoxo, your C++ bestie πŸ’… β€” source: http://cppreference.com, version/date: 2024.06.10, page: std::ranges..._view::size(3).

πŸ“¦ where this page comes from