std::ranges...w::drop_view(3)
π NAME π
ok so this is the std::ranges::drop_view::drop_view page from the C++ Standard Libary manual, section 3 β yes, literally just the constructors of drop_view, and yes they're iconic. It's the "make a view that skips the first count things" moment, and we're gonna walk through it together, bestie π«Ά
β¨ SYNOPSIS β¨
Here's the whole vibe, exactly as printed, no cap:
drop_view() requires std::default_initializable<V> = default;
(1) (since C++20)
constexpr explicit drop_view( V base,
(2) (since C++20)
ranges::range_difference_t<V> count );
πΈ DESCRIPTION πΈ
So basically: this constructor constructs a drop_view. That's it. That's the whole thing. But let me break down the two flavors, because they hit different π
- The default constructor. Zero-arg, no thoughts, head empty β it value-initializes the underlying view and initializes the
countto0. After construction,base()returns a copy ofV()andsize()equals the size of the underlying view. So nothing gets dropped, it's giving "I woke up and chose peace" energy π§ - The two-parameter constructor. This one's the main character: it initializes the underlying view with
std::move(base)and the count withcount. After construction,base()returns a copy ofbase, andsize()returnsranges::size(base) - countif the size ofbaseis not less thancount, or0otherwise. So if you ask it to skip more than it's got... it just gives you0. Graceful. Demure. Mindful π
π PARAMETERS π
baseβ the underlying view, the source material, the origin story β¨countβ number of elements to skip. Like scrolling past the first few posts fr fr
π₯ EXAMPLE π₯
The page says "// Run this code", so run it, bestie:
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <ranges>
int main()
{
constexpr std::array hi{'H', 'e', 'l', 'l', 'o', ',',
' ', 'C', '+', '+', '2', '0'};
std::ranges::for_each(hi, [](const char c){ std::cout << c; });
std::cout << '\n';
constexpr auto n = std::distance(hi.cbegin(), std::ranges::find(hi,
'C'));
auto cxx = std::ranges::drop_view{hi, n};
std::ranges::for_each(cxx, [](const char c){ std::cout << c; });
std::cout << '\n';
}
π Output:
Hello, C++20
C++20
look at that. it dropped everything before the C and the result is literally C++20. ate and left no crumbs π
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards β aka the committee went back and fixed the past, which is such a flex:
LWG 3714β applied to C++20. Behavior as published: the multi-parameter constructor was not made explicit. Correct behavior: it was madeexplicit(that's the(P2711R1)note next to it). So yeah, it's explicit now, periodt.
ngl, the one thing to remember: constructor (1) is defaulted but requiresstd::default_initializable<V>, and constructor (2) isconstexpr explicit. Don't come for me if you forget theexplicitβ the DR already handled that for you π
okay that's the whole page, every flag and flagless detail intact, I promise π«Ά this was brought to you by http://cppreference.com, version 2024.06.10 β mwah, go drop some views bestie πβ¨π
π¦ 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