You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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]>
Copy file name to clipboardExpand all lines: docs/cpp2/expressions.md
+35-8Lines changed: 35 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -212,11 +212,13 @@ For more examples, see also the examples in the previous two sections on `is` an
212
212
213
213
## <a id="captures"></a> `$` — captures, including interpolations
214
214
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.
216
216
217
217
`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.
218
218
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).
220
222
221
223
222
224
### <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
225
227
226
228
For example:
227
229
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"
229
231
main: () = {
230
-
s := "-sh\n";
232
+
s := "-ish\n";
231
233
vec: std::vector = (1, 2, 3, 5, 8, 13 );
232
234
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'
236
239
);
237
240
}
241
+
242
+
// prints:
243
+
// 1-ish
244
+
// 2-ish
245
+
// 3-ish
246
+
// 5-ish
247
+
// 8-ish
248
+
// 13-ish
238
249
```
239
250
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
+
```
241
268
242
269
243
270
### <aid="postcondition-captures"></a> Capture in contract postconditions
0 commit comments