lements_of&action=edit&section=1 "Edit
section: Template parameters") ]  Template parameters

R  |  \-  |  a type that satisfies [ ` range ` ](/w/cpp/ranges/range "cpp/ranges/range")  
---|---|---  
Allocator  |  \-  |  an allocator type that meets the requirements of [ Allocator  ](/w/cpp/named_req/Allocator "cpp/named req/Allocator")  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/elements_of&action=edit&section=2 "Edit
section: Data members") ]  Data members

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

All these members are declared with ` [[ [ no_unique_address
](/w/cpp/language/attributes/no_unique_address "cpp/language/attributes/no
unique address") ]] ` attribute.

###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/elements_of&action=edit&section=3 "Edit
section: Deduction guide") ]  Deduction guide

template  < class  R,  class  Allocator  =  [ std::  allocator
](http://en.cppreference.com/w/cpp/memory/allocator) < [ std::  byte
](http://en.cppreference.com/w/cpp/types/byte) > >  
elements_of  (  R  && , Allocator  =  Allocator  (  )  )  \-  > elements_of  < R  && , Allocator  > ;  |  |  (since C++23)   
---|---|---  
|  |   
  
###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/elements_of&action=edit&section=4 "Edit
section: Example") ]  Example

Run this code

    
    
    #include <any>
    #include <generator>
    #include <iostream>
    #include <ranges>
    #include <string_view>
     
    template<bool Elementwise>
    [std::generator](http://en.cppreference.com/w/cpp/coroutine/generator)<[std::any](http://en.cppreference.com/w/cpp/utility/any)> gen(std::[ranges::input_range](http://en.cppreference.com/w/cpp/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](http://en.cppreference.com/w/cpp/string/basic_string_view){"test"};
     
        for ([std::any](http://en.cppreference.com/w/cpp/utility/any) a : gen<true>(test))
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '[' << [std::any_cast](http://en.cppreference.com/w/cpp/utility/any/any_cast)<char>(a) << "] ";
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '\n';
     
        for ([std::any](http://en.cppreference.com/w/cpp/utility/any) a : gen<false>(test))
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '[' << [std::any_cast](http://en.cppreference.com/w/cpp/utility/any/any_cast)<[std::string_view](http://en.cppreference.com/w/cpp/string/basic_string_view)>(a) << "] ";
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '\n';
    }

Output:

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

###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/elements_of&action=edit&section=5 "Edit
section: References") ]  References

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

    

  * 26.5.6 Class template elements_of [range.elementsof] 

Retrieved from " [
https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/elements_of&oldid=170893
](https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/elements_of&oldid=170893)
"

#####  Navigation

  * [ Support us ](http://www.cppreference.com/support)
  * [ Recent changes ](/w/Special:RecentChanges "A list of recent changes in the wiki \[r\]")
  * [ FAQ ](/w/Cppreference:FAQ)
  * [ Offline version ](/w/Cppreference:Archives)

#####  Toolbox

  * [ What links here ](/w/Special:WhatLinksHere/cpp/ranges/elements_of "A list of all wiki pages that link here \[j\]")
  * [ Related changes ](/w/Special:RecentChangesLinked/cpp/ranges/elements_of "Recent changes in pages linked from this page \[k\]")
  * [