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

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

✨ std::ranges::common_view::size ✨

okay so real talk, this is a page from the C++ Standard Library manpage set, and it's all about std::ranges::common_view::size β€” literally the member function that tells you how many things are in there. it's giving "just count the elements" but make it constexpr. πŸ’…


🌸 NAME 🌸

std::ranges::common_view::size β€” yes bestie, it's std::ranges::common_view::size. that's the whole name. we love a self-explanatory icon. πŸ‘‘


πŸŽ€ 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 there's two overloads, and both are constexpr (yes, compile-time slay 🫢). notice the requires clauses β€” overload (1) only shows up if V is a ranges::sized_range, and overload (2) only shows up if const V is a ranges::sized_range. no sized range? no size(). the vibe check is in the constraints, fr fr.

it returns the number of elements. that's it. that's the function. ✨

it's equivalent to return ranges::size(base_);, where base_ is the underlying view. so it's not doing any secret extra math, it's just forwarding to ranges::size on the base. no cap. πŸ’€

πŸ¦‹ Parameters πŸ¦‹

  • (none) β€” nothing to pass in, bestie. it takes zero arguments. the way that's the easiest function signature ever. 😌

🌈 Return value 🌈

the number of elements. that's the tea. that's the whole return value. πŸ“


πŸ’– Example πŸ’–

// Run this code

#include <ranges>
#include <string_view>

int main()
{
    constexpr static auto v1 = {1, 2, 3, 4, 5};
    constexpr auto common1 { v1 | std::views::common };
    static_assert(common1.size() == 5);

    constexpr auto take3 { v1 | std::views::reverse | std::views::take(3) };
    constexpr auto common2 { take3 | std::views::common };
    static_assert(common2.size() == 3);

    using namespace std::literals;
    constexpr static auto v2 = { "∧"sv, "∨"sv, "βˆƒ"sv, "βˆ€"sv };
    static_assert(std::ranges::views::common(v2).size() == 4);
}

like, look at those static_asserts β€” everything's checked at compile time and the counts are 5, 3, and 4. iconic behavior honestly. πŸ”₯ the last one uses the logical symbols "∧"sv, "∨"sv, "βˆƒ"sv, "βˆ€"sv and yes it still counts to 4, because size is size no matter how cute your elements are. πŸ‘‘


🌟 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)

so if you want the unsigned-ish crowd you hit up ranges::size, and if you want a signed integer you go hang with ranges::ssize. both customization point objects, both C++20, both absolute legends. 🧚


later bestie!! this page came from http://cppreference.com and it's dated 2024.06.10 β€” go read more manpages, ily, periodt πŸ’œπŸ¦‹

πŸ“¦ where this page comes from