Skip to content

Commit 8d49525

Browse files
Update Capture sections in expressions.md (#987)
* Update expressions.md Signed-off-by: Neil Henderson <[email protected]> * Update highlighted linenos And a couple of other fixes, including that I meant to write "ish" not "sh" (fixing my own typo!) --------- Signed-off-by: Neil Henderson <[email protected]> Signed-off-by: Herb Sutter <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent 8d19818 commit 8d49525

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

docs/cpp2/expressions.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,13 @@ For more examples, see also the examples in the previous two sections on `is` an
212212
213213
## <a id="captures"></a> `$` — captures, including interpolations
214214
215-
Suffix `$` is pronounced **"paste the value"** and captures the value of an expression at the point when the expression where the capture is written is evaluated. Depending the complexity of the capture expression `expr$` and where it is used, parentheses `(expr)$` may be required for precedence or to show the boundaries of the expression.
215+
Suffix `$` is pronounced **"paste the value of"** and captures the value of an expression at the point when the expression where the capture is written is evaluated. Depending on the complexity of the capture expression `expr$` and where it is used, parentheses `(expr)$` may be required for precedence or to show the boundaries of the expression.
216216
217217
`x$` always captures `x` by value. To capture by reference, take the address and capture a pointer using `x&$`. If the value is immediately used, dereference again; for example `:(val) total&$* += val` adds to the `total` local variable itself, not a copy.
218218
219-
Any capture is evaluated at the point where it is written, in the function expression, contract postcondition, or string literal. The stored captured value can then be used later when the context it is in is evaluated, such as when the function expression body it's in is actually called later (one or more times), when the postcondition it's in is evaluated later when the function returns, or when the string literal it's in is read later.
219+
Captures are evaluated at the point where they are written in function expressions, contract postconditions, and string literals. The stored captured value can then be used later when evaluating its context, such as when the function expression body containing the captured value is actually called later (one or more times), when the postcondition containing the captured value is evaluated later when the function returns, or when the string literal containing the captured value is read later.
220+
221+
The design and syntax are selected so that capture is spelled the same way in all contexts. For details, see [Design note: Capture](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Capture).
220222
221223
222224
### <a id="function-captures"></a> Capture in function expressions (aka lambdas)
@@ -225,19 +227,44 @@ Any capture in a function expression body is evaluated at the point where the fu
225227
226228
For example:
227229
228-
``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="6"
230+
``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="7 8 13-18"
229231
main: () = {
230-
s := "-sh\n";
232+
s := "-ish\n";
231233
vec: std::vector = (1, 2, 3, 5, 8, 13 );
232234
233-
vec.std::ranges::for_each(
234-
:(i) = { std::cout << i << s$; }
235-
// Paste the value of `s`
235+
std::ranges::for_each(
236+
vec,
237+
:(i) = std::cout << i << s$
238+
// Function capture: Paste the value of 's'
236239
);
237240
}
241+
242+
// prints:
243+
// 1-ish
244+
// 2-ish
245+
// 3-ish
246+
// 5-ish
247+
// 8-ish
248+
// 13-ish
238249
```
239250

240-
The design and syntax are selected so that capture is spelled the same way in all contexts. For details, see [Design note: Capture](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Capture).
251+
Another example:
252+
253+
``` cpp title="Capture in a named function expression (aka lambda)" hl_lines="2 4 7 12 13"
254+
main: () = {
255+
price := 100;
256+
func := :() = {
257+
std::cout << "Price = " << price$ << "\n";
258+
};
259+
func();
260+
price = 200;
261+
func();
262+
}
263+
264+
// prints:
265+
// Price = 100
266+
// Price = 100
267+
```
241268

242269

243270
### <a id="postcondition-captures"></a> Capture in contract postconditions

0 commit comments

Comments
 (0)