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
// This program will print "caught something else"
396
396
main: () = {
397
397
try {
@@ -407,6 +407,39 @@ main: () = {
407
407
```
408
408
409
409
410
+
### <aid="static"></a> `#!cpp static` — Global variables inside functions
411
+
412
+
**`#!cpp static`** can be specified as the first token of a local variable declaration at function scope, to denote that the variable is a global value initialized thread-safely the first time the line is executed (aka C++ "function local static," aka "magic static").
413
+
414
+
For example (see also [`@singleton`](metafunctions.md#singleton)):
415
+
416
+
```cpp title="static variable (function local scope only)" hl_lines="15"
417
+
// For exposition only - normally, use @singleton for convenience
## <aid="definite-last-use"></a> Move/forward from definite last use
411
444
412
445
In a function body, a **definite last use** of a local name is a single use of that name in a statement that is not in a loop, where no control flow path after that statement mentions the name again.
Copy file name to clipboardExpand all lines: docs/cpp2/metafunctions.md
+32-1Lines changed: 32 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -365,13 +365,44 @@ main: () = {
365
365
```
366
366
367
367
368
+
### For other common types
369
+
370
+
#### `encapsulated`
371
+
372
+
An `encapsulated` type is one that has no public data members.
373
+
374
+
375
+
#### `noncopyable`
376
+
377
+
A `noncopyable` type is one that has no user-defined copy or move functions (`operator=` with `this` and `that` parameters).
378
+
379
+
380
+
#### <aid="singleton"></a> `singleton`
381
+
382
+
A `singleton` type is one that has only one global object (instance) in the whole program. The single object is initialized lazily the first time it is accessed. A singleton type has no constructors other than a generated default constructor (note: therefore all data members must have default values), and provides a generated `instance()` function to create and give access to the global instance.
383
+
384
+
For example:
385
+
386
+
```cpp title="A templated custom safe union type" hl_lines="1 7"
387
+
my_singleton: @singleton type = {
388
+
value: int = 42;
389
+
print: (this) = std::cout << "(value)$\n";
390
+
}
391
+
392
+
main: () = {
393
+
my_singleton::instance().print();
394
+
}
395
+
```
396
+
397
+
368
398
### For computational and functional types
369
399
370
400
#### `autodiff`
371
401
372
402
An `autodiff` type is extended so that derivatives can be computed. The metafunction adds for each function and member function a differentiated version. **This is a proof of concept implementation. Expect it to break.**
0 commit comments