class Ref,
class V = void,
class Allocator = void >
class generator
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 ranges::elements_of(rng), each element of the range
rng is successively produced as an element of the sequence.
std::generator
models view
and input_range
.
The behavior of a program that adds a specialization for std::generator
is undefined.
V
is void, both the reference type and the value type are inferred from Ref
V - the value type (ranges::range_value_t) of the generator, or void Allocator - an allocator type or void
If Allocator
is not void, then the behavior is undefined if Allocator
does not meet the Allocator requirements.
The program is ill-formed if any of these type requirements is not satisfied.
[edit] Data members Member Definitionactive_
(private)
Internally, each active instance of std::generator
is associated with a stack (handled as if by object of type std::unique_ptr<std::stack<std::coroutine_handle<>>>).
begin
is called, a new stack is created and the generator is added to the stack.coroutine_
(private) a handle of type std::coroutine_handle<promise_type>
generator
object
generator
s
generator
object
sized_range
or forward_range
std::ranges::view_interface<D>
) [edit] returns a constant iterator to the beginning of the range
std::ranges::view_interface<D>
) [edit] returns a sentinel for the constant iterator of the range
std::ranges::view_interface<D>
) [edit] returns whether the derived view is not empty, provided only if ranges::empty is applicable to it
std::ranges::view_interface<D>
) [edit] [edit] Nested classes the promise type
#include <generator> #include <iostream> template<typename T> struct Tree { T value; Tree *left{}, *right{}; std::generator<const T&> traverse_inorder() const { if (left) co_yield std::ranges::elements_of(left->traverse_inorder()); co_yield value; if (right) co_yield std::ranges::elements_of(right->traverse_inorder()); } }; 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 << x << ' '; std::cout << '\n'; }
Output:
[edit] ReferencesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4