Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::range_adaptor_closure

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      template<class D>

          requiresstd::is_object_v<D>&&std::same_as<D,std::remove_cv_t<D>>

      class range_adaptor_closure{};
      (since C++23)

      std::ranges::range_adaptor_closure is a helper class template for defining aRangeAdaptorClosureObject.

      Lett be the object of typeT, the implementation ensures thatt is a range adaptor closure object if all the requirements are met:

      • t is a unary function object that takes onerange argument.
      • T has exactly one public base classranges::range_adaptor_closure<T>, andT has no base classes of typeranges::range_adaptor_closure<U> for any other typeU.
      • T does not satisfyrange.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_ranges202202L(C++23)std::ranges::range_adaptor_closure

      [edit]Example

      Run this code
      #include <ranges>#include <string_view> // Define Slice as a range adaptor closurestruct Slice: std::ranges::range_adaptor_closure<Slice>{std::size_t start=0;std::size_t end=std::string_view::npos; constexprstd::string_view operator()(std::string_view sv)const{return sv.substr(start, end- start);}}; int main(){constexprstd::string_view str="01234567"; constexpr Slice slicer{.start=1, .end=6}; // use slicer as a normal function objectconstexprauto sv1= slicer(str);    static_assert(sv1=="12345"); // use slicer as a range adaptor closure objectconstexprauto sv2= str| slicer;    static_assert(sv2=="12345"); // range adaptor closures can be composedconstexprauto slice_and_drop= slicer| std::views::drop(2);    static_assert(std::string_view(str| slice_and_drop)=="345");}
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/range_adaptor_closure&oldid=176010"

      [8]ページ先頭

      ©2009-2025 Movatter.jp