A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../ranges/../ranges/range_adaptor_closure.html below:

std::ranges::range_adaptor_closure - cppreference.com

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:

[edit] Notes [edit] Example
#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};
 
    // use slicer as a normal function object
    constexpr auto sv1 = slicer(str);
    static_assert(sv1 == "12345");
 
    // use slicer as a range adaptor closure object
    constexpr auto sv2 = str | slicer;
    static_assert(sv2 == "12345");
 
    // range adaptor closures can be composed
    constexpr auto slice_and_drop = slicer | std::views::drop(2);
    static_assert(std::string_view(str | slice_and_drop) == "345");
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4