Files
2025-10-25 03:02:53 +03:00

29 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[expr.yield]
# 7 Expressions [[expr]](./#expr)
## 7.6 Compound expressions [[expr.compound]](expr.compound#expr.yield)
### 7.6.17 Yielding a value [expr.yield]
[yield-expression:](#nt:yield-expression "7.6.17Yielding a value[expr.yield]")
co_yield [*assignment-expression*](expr.assign#nt:assignment-expression "7.6.19Assignment and compound assignment operators[expr.assign]")
co_yield [*braced-init-list*](dcl.init.general#nt:braced-init-list "9.5.1General[dcl.init.general]")
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7971)
A [*yield-expression*](#nt:yield-expression "7.6.17Yielding a value[expr.yield]") shall appear only within a suspension context
of a function ([[expr.await]](expr.await "7.6.2.4Await"))[.](#1.sentence-1)
Let *e* be the operand of the [*yield-expression*](#nt:yield-expression "7.6.17Yielding a value[expr.yield]") and*p* be an lvalue naming the promise object of the enclosing
coroutine ([[dcl.fct.def.coroutine]](dcl.fct.def.coroutine "9.6.4Coroutine definitions")), then the [*yield-expression*](#nt:yield-expression "7.6.17Yielding a value[expr.yield]") is equivalent to the expressionco_await *p*.yield_value(*e*)[.](#1.sentence-2)
[*Example [1](#example-1)*: template <typename T>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*](#nt:yield-expression "7.6.17Yielding a value[expr.yield]") outside of function suspension contextint a[] = { co_yield 1 }; // error: [*yield-expression*](#nt:yield-expression "7.6.17Yielding a value[expr.yield]") 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*]