###  [ [ edit
](/mwiki/index.php?title=cpp/coroutine/generator&action=edit&section=6 "Edit
section: Notes") ]  Notes

[ Feature-test ](/w/cpp/utility/feature_test "cpp/utility/feature test") macro  |  Value  |  Std  |  Feature   
---|---|---|---  
[ ` __cpp_lib_generator ` ](/w/cpp/feature_test#cpp_lib_generator "cpp/feature test") |  202207L  |  (C++23)  |  ` std::generator ` – synchronous [ coroutine ](/w/cpp/coroutine "cpp/coroutine") generator for [ ranges ](/w/cpp/ranges "cpp/ranges")  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/coroutine/generator&action=edit&section=7 "Edit
section: Example") ]  Example

Can be tried in [ Compiler Explorer ](https://godbolt.org/z/sj9ed1ETb)

Run this code

    
    
    #include <generator>
    #include <iostream>
     
    template<typename T>
    struct Tree
    {
        T value;
        Tree *left{}, *right{};
     
        std::generator<const T&> traverse_inorder() const
        {
            if (left)
                for (const T& x : left->traverse_inorder())
                    co_yield x;
     
            co_yield value;
            if (right)
                for (const T& x : right->traverse_inorder())
                    co_yield x;
        }
    };
     
    int main()
    {
        Tree<char> tree[]
        {
                                        {'D', tree + 1, tree + 2},
            //                            │
            //            ┌───────────────┴────────────────┐
            //            │                                │
                        {'B', tree + 3, tree + 4},       {'F', tree + 5, tree + 6},
            //            │                                │
            //  ┌─────────┴─────────────┐      ┌───────────┴─────────────┐
            //  │                       │      │                         │
              {'A'},                  {'C'}, {'E'},                    {'G'}
        };
     
        for (char x : tree->traverse_inorder())
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << x << ' ';
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '\n';
    }

Output:

    
    
    A B C D E F G

###  [ [ edit
](/mwiki/index.php?title=cpp/coroutine/generator&action=edit&section=8 "Edit
section: References") ]  References

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

    

  * 26.8 Range generators [coro.generator] 

###  [ [ edit
](/mwiki/index.php?title=cpp/coroutine/generator&action=edit&section=9 "Edit
section: See also") ]  See also

[ noop_coroutine  ](/w/cpp/coroutine/noop_coroutine "cpp/coroutine/noop coroutine") (C++20)  |  creates a coroutine handle that has no observable effects when resumed or destroyed   
(function)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/coroutine/dsc_noop_coroutine&action=edit)  
---|---  
  
Retrieved from " [
https://en.cppreference.com/mwiki/index.php?title=cpp/coroutine/generator&oldid=170892
](https://en.cppreference.com/mwiki/index.php?title=cpp/coroutine/generator&oldid=170892)
"

#####  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/coroutine/generator "A list of all wiki pages that link here \[j\]")
  * [ Related changes ](/w/Special:RecentChangesLinked/cpp/coroutine/generator "Recent changes in pages linked from this page \[k\]")
  * [ Upload file ](http://upload.cppreference.com/w/Special:Upload "Upload files \[u\]")
  * [ Special pages ](/w/Special:SpecialPages "A list of all special pages \[q\]")
  * [ Printable version ](/mwiki/index.php?title=cpp/coroutine/generator&printable=yes "Printable version of this page \[p\]")
  * [ Permanent link ](/mwiki/index.php?title=cpp/coroutine/generator&oldid=170892 "Permanent link to this revision of the page")
  * [ Page informat