From Jason Turner

[expr.yield]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmps9mc_p1z/{from.md → to.md} +52 -0
tmp/tmps9mc_p1z/{from.md → to.md} RENAMED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Yielding a value <a id="expr.yield">[[expr.yield]]</a>
2
+
3
+ ``` bnf
4
+ yield-expression:
5
+ 'co_yield' assignment-expression
6
+ 'co_yield' braced-init-list
7
+ ```
8
+
9
+ A *yield-expression* shall appear only within a suspension context of a
10
+ function [[expr.await]]. Let *e* be the operand of the
11
+ *yield-expression* and *p* be an lvalue naming the promise object of the
12
+ enclosing coroutine [[dcl.fct.def.coroutine]], then the
13
+ *yield-expression* is equivalent to the expression
14
+ `co_await `*p*`.yield_value(`*e*`)`.
15
+
16
+ [*Example 1*:
17
+
18
+ ``` cpp
19
+ template <typename T>
20
+ struct my_generator {
21
+ struct promise_type {
22
+ T current_value;
23
+ ...
24
+ auto yield_value(T v) {
25
+ current_value = std::move(v);
26
+ return std::suspend_always{};
27
+ }
28
+ };
29
+ struct iterator { ... };
30
+ iterator begin();
31
+ iterator end();
32
+ };
33
+
34
+ my_generator<pair<int,int>> g1() {
35
+ for (int i = i; i < 10; ++i) co_yield {i,i};
36
+ }
37
+ my_generator<pair<int,int>> g2() {
38
+ for (int i = i; i < 10; ++i) co_yield make_pair(i,i);
39
+ }
40
+
41
+ auto f(int x = co_yield 5); // error: yield-expression outside of function suspension context
42
+ int a[] = { co_yield 1 }; // error: yield-expression outside of function suspension context
43
+
44
+ int main() {
45
+ auto r1 = g1();
46
+ auto r2 = g2();
47
+ assert(std::equal(r1.begin(), r1.end(), r2.begin(), r2.end()));
48
+ }
49
+ ```
50
+
51
+ — *end example*]
52
+