Files
cppdraft_translate/cppdraft/expr/yield.md
2025-10-25 03:02:53 +03:00

2.1 KiB

[expr.yield]

7 Expressions [expr]

7.6 Compound expressions [expr.compound]

7.6.17 Yielding a value [expr.yield]

yield-expression:
co_yield assignment-expression
co_yield braced-init-list

1

#

A yield-expression shall appear only within a suspension context of a function ([expr.await]).

Let e be the operand of the yield-expression andp be an lvalue naming the promise object of the enclosing coroutine ([dcl.fct.def.coroutine]), then the yield-expression is equivalent to the expressionco_await p.yield_value(e).

[Example 1: template struct my_generator {struct promise_type { T current_value; /* ... /auto yield_value(T v) { current_value = std::move(v); return std::suspend_always{}; }}; struct iterator { / ... */ }; iterator begin(); iterator end();};

my_generator<pair<int,int>> g1() {for (int i = 0; i < 10; ++i) co_yield {i,i};} my_generator<pair<int,int>> g2() {for (int i = 0; i < 10; ++i) co_yield make_pair(i,i);}auto f(int x = co_yield 5); // error: yield-expression outside of function suspension contextint a[] = { co_yield 1 }; // error: yield-expression outside of function suspension contextint main() {auto r1 = g1(); auto r2 = g2(); assert(std::equal(r1.begin(), r1.end(), r2.begin(), r2.end()));} — end example]