// std::generator<X>,co_yield出来的数据类型是X&&std::generator<X>always_ref_example(){co_yieldX{1};// 右值,不会发生拷贝{Xx{2};co_yieldx;// 左值,会发生拷贝assert(x.id==2);}{constXx{3};co_yieldx;// const X&,也会发生拷贝assert(x.id==3);}{Xx{4};co_yieldstd::move(x)...
(二、promise_type初探:yield_value的重载决议) 上文提到_Promise_erased中有两个数据成员。其中,我们很顺理成章的推测_Subyield_state _M_nest与嵌套调用有关。接下来,我们将会考察调用栈中,协程的handle是如何存储在promise中的,co_yield产生的结果是如何保存下来的。我们用栈底协程指代最外层的generator协程,是...
中文 English Home > Product > Default Product STD GeneratorPhone Share Top
:publicranges::view_interface<generator<Ref, V, Allocator>> (1)(since C++23) namespacepmr{ template<classRef,classV=void> usinggenerator= std::generator<Ref, V,std::pmr::polymorphic_allocator<>>; } (2)(since C++23) 1)The class templatestd::generatorpresents aviewof the elements yielded...
std::generator::iterator::operator= /*iterator*/&operator=(/*iterator*/&&other)noexcept; (since C++23) Equivalent tocoroutine_=std::exchange(other.coroutine_,{});. Returns:*this. std::generator::iterator::operator* reference operator*()const ...
当然,C++ 毕竟是一个静态类型的语言,除了 range 函数要写 “返回值” generator<int> (当然实际上 range 是一个协程,代码 81 行只是借用了原来的函数和返回值语法了,严格来说不能认为这里声明了一个返回 generator<int> 类型的函数) ,以及 90 行需要声明 gen 的类型以外, 可以说和 Python 没什么两样了。
generator& operator=( generator other ) noexcept; (C++23 起) 替换生成器对象的内容。等价于: std::swap(coroutine_, other.coroutine_); std::swap(active_, other.active_); 参数other - 要被移动的生成器对象 返回值*this 复杂度本节未完成 ...
template< class ExecutionPolicy, class ForwardIt, class Generator > void generate( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Generator g ); (2) (C++17 起) 1) 以给定函数对象 g 所生成的值对范围 [first, last) 中的每个元素赋值。2...
我的理解可能是generator_iterator无法处理co_await,因为它需要使用co_yeld暂停任务,请参阅迭代器源代码: generator_iterator& operator++() { m_coroutine.resume(); if (m_coroutine.done()) { m_coroutine.promise().rethrow_if_exception(); }
constexpr OutputIt generate_n( OutputIt first, Size count, Generator g ); (C++20 起) template< class ExecutionPolicy, class ForwardIt , class Size, class Generator > ForwardIt generate_n( ExecutionPolicy&& policy, ForwardIt first, Size count, Generator g ); (2) (C++17 起) 1...