std::ranges..._view::size(3)
β¨ 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 toreturn ranges::size(base_);, wherebase_is the underlying view. so it's not doing any secret extra math, it's just forwarding toranges::sizeon 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms