|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Coroutine traits | ||||
(C++20) | ||||
| Coroutine handle | ||||
(C++20) | ||||
| No-op coroutines | ||||
(C++20) | ||||
(C++20) | ||||
| Trivial awaitables | ||||
(C++20) | ||||
(C++20) | ||||
| Range generators | ||||
generator (C++23) |
| ||||||||||||||||||||||
| Range primitives | |||||||
| |||||||
| Range concepts | |||||||||||||||||||
| |||||||||||||||||||
| Range factories | |||||||||
| |||||||||
| Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||
| Helper items | |||||||||||||||||
| |||||||||||||||||
Defined in header <generator> | ||
template< class Ref, | (1) | (since C++23) |
namespace pmr{ template<class Ref,class V=void> | (2) | (since C++23) |
std::generator presents aview of the elements yielded by the evaluation of acoroutine.Astd::generator generates a sequence of elements by repeatedly resuming the coroutine from which it was returned.Each time aco_yield statement is evaluated, the coroutine produces one element of the sequence.When theco_yield statement is of the formco_yield ranges::elements_of(rng), each element of therangerng is successively produced as an element of the sequence.
std::generator modelsview andinput_range.
The behavior of a program that adds a specialization forstd::generator is undefined.
Contents |
| Ref | - | the reference type (ranges::range_reference_t) of the generator. IfV isvoid, both the reference type and the value type are inferred fromRef |
| V | - | the value type (ranges::range_value_t) of the generator, orvoid |
| Allocator | - | an allocator type orvoid |
IfAllocator is notvoid, then the behavior is undefined ifAllocator does not meet theAllocator requirements.
| Member | Definition |
value(private) | std::conditional_t<std::is_void_v<V>,std::remove_cvref_t<Ref>, V>; (exposition-only member type*) |
reference(private) | std::conditional_t<std::is_void_v<V>, Ref&&, Ref>; (exposition-only member type*) |
yielded | std::conditional_t<std::is_reference_v<reference >, reference,const reference &> |
| Type requirements | ||
| -std::allocator_traits<Allocator>::pointer is a pointer type. | ||
-value is a cv-unqualified object type. | ||
-reference is either a reference type, or a cv-unqualified object type that modelscopy_constructible. | ||
-LetRRef denotestd::remove_reference_t<reference >&&, ifreference is a reference type, andreference otherwise.
|
The program is ill-formed if any of these type requirements is not satisfied.
| Member | Definition |
active_(private) | Internally, each active instance of
|
coroutine_(private) | a handle of typestd::coroutine_handle<promise_type> (exposition-only member object*) |
constructs agenerator object(public member function)[edit] | |
effectively destroys the entire stack of yieldedgenerators(public member function)[edit] | |
assigns agenerator object(public member function)[edit] | |
| resumes the initially suspended coroutine and returns an iterator to its handle (public member function)[edit] | |
| returnsstd::default_sentinel (public member function)[edit] | |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_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 only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D>)[edit] | |
| the promise type (public member class) | |
| the iterator type (exposition-only member class*) |
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_generator | 202207L | (C++23) | std::generator – synchronouscoroutine generator forranges |
#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:
A B C D E F G
(C++20) | creates a coroutine handle that has no observable effects when resumed or destroyed (function)[edit] |