std::ranges...tor_closure(3)
okay so we're on the std::ranges...tor_closure(3) page from the C++ Standard Libary manual β yes, βLibaryβ is literally how the header spells it, donβt come for me ππ
.
π NAME π
std::ranges::range_adaptor_closure - std::ranges::range_adaptor_closure
πΈ Synopsis πΈ
Defined in header <ranges>.
template< class D >
requires std::is_object_v<D> && std::same_as<D,
std::remove_cv_t<D>>
class range_adaptor_closure {};
(since C++23) β this is the C++23 era, bestie β¨
std::ranges::range_adaptor_closure is a helper class template for defining a RangeAdaptorClosureObject.
Let t be the object of type T; the implementation ensures that t is a range adaptor closure object if all the requirements are met:
tis a unary function object that takes one range argument.Thas exactly one public base classranges::range_adaptor_closure<T>, andThas no base classes of typeranges::range_adaptor_closure<U>for any other typeU.Tdoes not satisfyrange.
β¨ Example β¨
The page starts the example with // Run this code β here's the whole thing, no cap:
// Run this code
#include <ranges>
#include <string_view>
// Define Slice as a range adaptor closure
struct Slice : std::ranges::range_adaptor_closure<Slice>
{
std::size_t start = 0;
std::size_t end = std::string_view::npos;
constexpr std::string_view operator()(std::string_view sv) const
{
return sv.substr(start, end - start);
}
};
int main()
{
constexpr std::string_view str = "01234567";
constexpr Slice slicer{.start = 1, .end = 6};
constexpr auto sv1 = slicer(str); // use slicer as a normal function object
constexpr auto sv2 = str | slicer; // use slicer as a range adaptor closure object
static_assert(sv1 == "12345");
static_assert(sv2 == "12345");
// range adaptor closures can be composed
constexpr auto slice_and_drop
= slicer
| std::views::drop_while([](char ch) { return ch != '3'; });
static_assert(std::string_view(str | slice_and_drop) == "345");
}
mwah π β source: http://cppreference.com; date: 2024.06.10. That's the footer, folded into this cute little sign-off π«Ά
π¦ 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