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
Copy file name to clipboardExpand all lines: docs/cpp2/expressions.md
+10-9Lines changed: 10 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -212,7 +212,7 @@ For more examples, see also the examples in the previous two sections on `is` an
212
212
213
213
## `$` — captures, including interpolations
214
214
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 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"** 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.
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
@@ -225,14 +225,14 @@ Any capture in a function expression body is evaluated at the point where the fu
225
225
226
226
For example:
227
227
228
-
``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="6 7"
228
+
``` cpp title="Capture in an unnamed function expression (aka lambda)" hl_lines="6"
229
229
main: () = {
230
230
s := "-sh\n";
231
231
vec: std::vector = (1, 2, 3, 5, 8, 13 );
232
232
233
233
vec.std::ranges::for_each(
234
234
:(i) = { std::cout << i << s$; }
235
-
// Function capture: Paste local variable value
235
+
// Paste the value of `s`
236
236
);
237
237
}
238
238
```
@@ -246,10 +246,10 @@ Any capture in a postcondition is evaluated at the point where the postcondition
246
246
247
247
For example:
248
248
249
-
```cpp title="Capture in contract postconditions" hl_lines="2 3"
249
+
```cpp title="Capture in contract postconditions" hl_lines="2"
250
250
push_back: (coll, value)
251
251
[[post: coll.ssize() == coll.ssize()$ + 1]]
252
-
//Postcondition capture: Paste "old" size
252
+
// Paste the value of `coll.ssize()`
253
253
= {
254
254
// ...
255
255
}
@@ -258,22 +258,23 @@ push_back: (coll, value)
258
258
259
259
### Capture in string interpolation
260
260
261
-
A string literal can capture the value of an expression `expr` by writing `(expr)$` inside the string literal. The `(``)` are required, and cannot be nested.
261
+
A string literal can capture the value of an expression `expr` by writing `(expr)$` inside the string literal. The `(``)` are required, and cannot be nested. A string literal has type `std::string` if it performs any captures, otherwise it is a normal C/C++ string literal (array of characters).
262
262
263
263
Any capture in a string literal is evaluated at the point where the string literal is written. The string literal can be used repeatedly later, and includes the captured value.
264
264
265
265
For example:
266
266
267
-
```cpp title="Capture for string interpolation" hl_lines="2 4"
267
+
```cpp title="Capture for string interpolation" hl_lines="2 5"
268
268
x := 0;
269
269
std::cout << "x is (x)$\n";
270
+
// Paste the value of `x`
270
271
x = 1;
271
272
std::cout << "now x+2 is (x+2)$\n";
273
+
// Paste the value of `x+2`
272
274
273
275
// prints:
274
276
// x is 0
275
277
// now x+2 is 3
276
278
```
277
279
278
-
A string literal has type `std::string` if it performs any captures, otherwise it is a normal C/C++ string literal (array of characters).
279
-
280
+
A string literal capture can include a `:suffix` where the suffix is a [standard C++ format specification](https://en.cppreference.com/w/cpp/utility/format/spec). For example, `#!cpp (x.price(): <10.2f)$` evaluates `x.price()` and converts the result to a string with 10-character width, 2 digits of precision, and left-justified.
auto hello(cpp2::in<std::string_view> msg) -> void {
@@ -116,7 +106,7 @@ Here we can see more of how Cpp2 makes it features work.
116
106
- **Line 9: CTAD** just works, because it turns into ordinary C++ code which already supports CTAD.
117
107
- **Lines 10-11: Automatic bounds checking** is added to `#!cpp words[0]` and `#!cpp words[1]` nonintrusively at the call site by default. Because it's nonintrusive, it works seamlessly with all existing container types that are `std::size` and `std::ssize`-aware, when you use them from safe Cpp2 code.
118
108
- **Line 11: Automatic move from last use** ensures the last use of `words` will automatically avoid a copy if it's being passed to something that's optimized for rvalues.
119
-
- **Line 16: String interpolation** performs the string capture of `msg`'s current value via `cpp2::to_string`. That uses `std::to_string` when available, and it also works for additional types (such as `#!cpp bool`, to print `#!cpp false` and `#!cpp true` instead of `0` and `1`, without having to remember to use `std::boolalpha`).
109
+
- **Line 15: String interpolation** performs the string capture of `msg`'s current value via `cpp2::to_string`. That uses `std::to_string` when available, and it also works for additional types (such as `#!cpp bool`, to print `#!cpp false` and `#!cpp true` instead of `0` and `1`, without having to remember to use `std::boolalpha`).
120
110
121
111
**How: Simplicity through generality + defaults.**
122
112
@@ -128,7 +118,7 @@ Here we can see more of how Cpp2 makes it features work.
128
118
129
119
**How: Seamless compatibility and interop.**
130
120
131
-
- **Lines 9, 12, and 16: Ordinary direct calls** to existing C++ code, so there's never a need for wrapping/marshaling/thunking.
121
+
- **Lines 9-11 and 16: Ordinary direct calls** to existing C++ code, so there's never a need for wrapping/marshaling/thunking.
0 commit comments