std::ranges..._view::size(3)
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
- 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