std::ranges::elements_of - cppreference.com

From cppreference.com

template<ranges::rangeR,classAllocator=std::allocator<std::byte>>structelements_of;(since C++23)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 Description a range
(public member object)an allocator. It has default member initializer that value-initializes itself
(public member object)Deduction guide

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

Run this code

#include<any>#include<generator>#include<iostream>#include<ranges>#include<string_view>template<boolElementwise>std::generator<std::any>gen(std::ranges::input_rangeauto&&r){ifconstexpr(Elementwise)co_yieldstd::ranges::elements_of(r);// yield each element of relseco_yieldr;// yield r as a single value}intmain(){autotest=std::string_view{"test"};for(std::anya:gen<true>(test))std::cout<<'['<<std::any_cast<char>(a)<<"] ";std::cout<<'\n';for(std::anya: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:2024):

26.5.6 Class template elements_of [range.elementsof]