home | help
std::ranges::elements_of(3)    C++ Standard Libary   std::ranges::elements_of(3)

NAME
     std::ranges::elements_of - std::ranges::elements_of

Synopsis
	Defined in header <ranges>
	template< ranges::range R, class Allocator =
	std::allocator<std::byte>					       >
     (since C++23)
	struct elements_of;

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

Template parameters
	R	  - a type that satisfies range
	Allocator - an allocator type that meets the requirements of Allocator

	Data members

	Member name Definition
	range	    a range of type R
		    (public member object)
		    an	allocator  of type Allocator. It has default member ini-
     tializer that
	allocator   value initializes itself
		    (public member object)

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

	Deduction guide

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

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_range auto&& r)
      {
	  if constexpr (Elementwise)
	      co_yield std::ranges::elements_of(r); // yield each element of r
	  else
	      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]

References
	  * C++23 standard (ISO/IEC 14882:2023):

	  * 26.5.6 Class template elements_of [range.elementsof]

http://cppreference.com 	   2024.06.10	     std::ranges::elements_of(3)

home | help