Namespaces
Variants
Views
Actions

std::generator

From cppreference.com
< cpp‎ | coroutine
Revision as of 14:30, 2 February 2023 by Space Mission (Talk | contribs)

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Coroutine support
Coroutine traits
Coroutine handle
No-op coroutines
Trivial awaitables
Range generators
generator
(C++23)
 
Ranges library
Range adaptors
 
 
Defined in header <generator>
template<

    class Ref,
    class V = void,
    class Allocator = void
>
class generator :

    public ranges::view_interface<generator<Ref, V, Allocator>>
(since C++23)

The class template std::generator presents a view of the elements yielded by the evaluation of a coroutine.

A std::generator generates a sequence of elements by repeatedly resuming the coroutine from which it was returned. Each time a co_yield statement is evaluated, the coroutine produces one element of the sequence. When the co_yield statement is of the form co_yield elements_of(rng), each element of the range rng is successively produced as an element of the sequence.

Contents

Template parameters

Ref - TODO
V - TODO
Allocator - TODO

Data members

TODO

Member types

TODO

Member functions

TODO
Inherited from std::ranges::view_interface
returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a constant iterator to the beginning of the range.
(public member function of std::ranges::view_interface<D>) [edit]
(C++23)
returns a sentinel for the constant iterator of the range.
(public member function of std::ranges::view_interface<D>) [edit]
returns whether the derived view is not empty. Provided if ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>) [edit]

Nested classes

TODO

Notes

Feature-test macro Value Std Feature
__cpp_lib_generator 202207L (C++23) std::generator – synchronous coroutine generator for ranges

Example

#include <generator>
#include <ranges>
#include <iostream>
 
std::generator<char> letters(char first)
{
    for (;; co_yield first++);
}
 
int main()
{
    for (const char ch : letters('a') | std::views::take(13*2))
        std::cout << ch << ' ';
    std::cout << '\n';
}

Output:

a b c d e f g h i j k l m n o p q r s t u v w x y z

See also

creates a coroutine handle that has no observable effects when resumed or destroyed
(function) [edit]