Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::elements_of

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      template<ranges::range R,class Allocator=std::allocator<std::byte>>
      struct elements_of;
      (since C++23)

      Encapsulates arange. Specializations ofelements_of act as a tag in overload sets to disambiguate when a range should be treated as a sequence rather than a single value.

      Contents

      [edit]Template parameters

      R - a type that satisfiesrange
      Allocator - an allocator type that meets the requirements ofAllocator

      [edit]Data members

      Member name Definition
      range
      a range of typeR
      (public member object)
      allocator
      an allocator of typeAllocator. It has default member initializer that value initializes itself
      (public member object)

      All these members are declared with[[no_unique_address]] attribute.

      [edit]Deduction guide

      template<class R,class Allocator=std::allocator<std::byte>>
      elements_of( R&&, Allocator= Allocator())-> elements_of<R&&, Allocator>;
      (since C++23)

      [edit]Example

      Run this code
      #include <any>#include <generator>#include <iostream>#include <ranges>#include <string_view> template<bool Elementwise>std::generator<std::any> gen(std::ranges::input_rangeauto&& r){ifconstexpr(Elementwise)        co_yield std::ranges::elements_of(r);// yield each element of relse        co_yield r;// yield r as a single value} int main(){auto test=std::string_view{"test"}; for(std::any a: gen<true>(test))std::cout<<'['<<std::any_cast<char>(a)<<"] ";std::cout<<'\n'; for(std::any a: gen<false>(test))std::cout<<'['<<std::any_cast<std::string_view>(a)<<"] ";std::cout<<'\n';}

      Output:

      [t] [e] [s] [t] [test]

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 26.5.6 Class template elements_of [range.elementsof]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/elements_of&oldid=171338"

      [8]ページ先頭

      ©2009-2025 Movatter.jp