std::ranges::range_adaptor_closure - cppreference.com

From cppreference.com

template<classD>requiresstd::is_object_v<D>&&std::same_as<D,std::remove_cv_t<D>>classrange_adaptor_closure{};(since C++23)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:

t is a unary function object that takes one

range

argument.

T has exactly one public base class ranges::range_adaptor_closure<T>, and T has no base classes of type ranges::range_adaptor_closure<U> for any other type U.

T does not satisfy

range

.

Notes

Feature-test

macro ValueStdFeature

__cpp_lib_ranges

202202L

(C++23)std::ranges::range_adaptor_closureExample

Run this code

#include<ranges>#include<string_view>// Define Slice as a range adaptor closurestructSlice:std::ranges::range_adaptor_closure<Slice>{std::size_tstart=0;std::size_tend=std::string_view::npos;constexprstd::string_viewoperator()(std::string_viewsv)const{returnsv.substr(start,end-start);}};intmain(){constexprstd::string_viewstr="01234567";constexprSliceslicer{.start=1,.end=6};// use slicer as a normal function objectconstexprautosv1=slicer(str);static_assert(sv1=="12345");// use slicer as a range adaptor closure objectconstexprautosv2=str|slicer;static_assert(sv2=="12345");// range adaptor closures can be composedconstexprautoslice_and_drop=slicer|std::views::drop(2);static_assert(std::string_view(str|slice_and_drop)=="345");}