std::ranges...e_view::size(3)
π NAME π
std::ranges::enumerate_view::size - std::ranges::enumerate_view::size (yes bestie, the name is the name twice, iconic behavior).
Okay so this is the std::ranges...e_view::size(3) page from the C++ Standard Libary manual. Weβre talking about the size() member of std::ranges::enumerate_view, and it is giving βhow many elements are in this view?β energy. Periodt. π
β¨ Synopsis β¨
Two overloads, both constexpr, both since C++23. Vibe check passed:
constexpr auto size() requires ranges::sized_range<V>;
(1) (since C++23)
constexpr auto size() const requires ranges::sized_range<const V>;
(2) (since C++23)
π Parameters π
(none) β literally none, zero, the void. No arguments, no stress. π«Ά
πΈ Return value πΈ
The number of elements. Thatβs it, thatβs the tea. ββ¨
Under the hood itβs equivalent to return ranges::size(base_);, where base_ is the underlying view. Slay. π
π¦ Example π¦
The page literally says // Run this code, so run it, bestie:
#include <cassert>
#include <forward_list>
#include <ranges>
#include <string_view>
int main()
{
constexpr static auto v1 = {1, 2, 3, 4, 5};
constexpr auto ev1{v1 | std::views::enumerate};
static_assert(ev1.size() == 5);
static_assert(std::ranges::sized_range<decltype(v1)>);
auto v2 = std::forward_list{1, 2, 3, 4, 5};
auto ev2 {v2 | std::views::enumerate};
static_assert(not std::ranges::sized_range<decltype(v2)>);
// auto size = ev2.size(); // Error: v2 is not a sized range
assert(std::ranges::distance(v2) == 5); // OK, distance does not
require sized
// range, but has O(N)
complexity here
}
Not me pointing at the commented-out line:// auto size = ev2.size(); // Error: v2 is not a sized rangeβ yeah, that one is an error on purpose, becausev2is not a sized range. Theassertline is fine becausedistancedoes not require sized range, but it has O(N) complexity here. No cap. π
π 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! Source/version: http://cppreference.com; date: 2024.06.10; page: std::ranges...e_view::size(3). Mwah πβ¨
π¦ 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