Skip to content

Commit 83582ad

Browse files
committed
Add function local statics + 'singleton' metaclass
And 'encapsulated' and 'noncopyable' metaclasses
1 parent 1715e15 commit 83582ad

20 files changed

+1285
-946
lines changed

docs/cpp2/functions.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ outer: while i<M next i++ { // loop named "outer"
391391

392392
For example:
393393

394-
``` cpp title="Throw, try, catch"
394+
``` cpp title="Throw, try, catch" hl_lines="1 3-5 9-11"
395395
// This program will print "caught something else"
396396
main: () = {
397397
try {
@@ -407,6 +407,39 @@ main: () = {
407407
```
408408

409409

410+
### <a id="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
418+
my_singleton: type = {
419+
value : int = 42;
420+
421+
print : (this) = std::cout << "(value)$\n";
422+
423+
//--------------------------------------------------------------------
424+
// writing '@singleton type' above would generate these two functions
425+
// but this example shows them for exposition of 'static'
426+
//
427+
private
428+
operator=: (out this) = { }
429+
430+
instance : () -> forward my_singleton = {
431+
static _instance: my_singleton = (); // singleton variable
432+
return _instance;
433+
}
434+
//--------------------------------------------------------------------
435+
}
436+
437+
main: () = {
438+
my_singleton::instance().print();
439+
}
440+
```
441+
442+
410443
## <a id="definite-last-use"></a> Move/forward from definite last use
411444

412445
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.

docs/cpp2/metafunctions.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,44 @@ main: () = {
365365
```
366366

367367

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+
#### <a id="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+
368398
### For computational and functional types
369399

370400
#### `autodiff`
371401

372402
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.**
373403
A simple hello diff example is:
374-
```
404+
405+
``` cpp
375406
ad: @autodiff type = {
376407
func: (x: double) -> (r: double) = {
377408
r = x * x;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
test: @singleton type = {
3+
value: int = 42;
4+
print: (this) = std::cout << "(value)$\n";
5+
}
6+
7+
main: () = {
8+
test::instance().print();
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

regression-tests/test-results/gcc-10-c++20/run-tests-gcc-10.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for f in *.cpp
1010
do
1111
let count=count+1
1212
printf "[%s] Starting gcc 10 %s\n" "$count" "$f"
13-
g++-10 -I../../../include -std=c++20 -pthread -Wold-style-cast -Wunused-parameter -o test.exe $f > $f.output 2>&1
13+
g++-10 -I../../../include -std=c++20 -pthread -Wold-style-cast -Wunused-parameter -Wno-unused-result -o test.exe $f > $f.output 2>&1
1414
rm -f $f
1515
if test -f "test.exe"; then
1616
let exe_count=exe_count+1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

regression-tests/test-results/gcc-14-c++2b/run-tests-gcc-14.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for f in *.cpp
1010
do
1111
let count=count+1
1212
printf "[%s] Starting gcc 14 %s\n" "$count" "$f"
13-
g++ -I../../../include -std=c++2b -pthread -Wold-style-cast -Wunused-parameter -o test.exe $f > $f.output 2>&1
13+
g++ -I../../../include -std=c++2b -pthread -Wold-style-cast -Wunused-parameter -Wno-unused-result -o test.exe $f > $f.output 2>&1
1414
rm -f $f
1515
if test -f "test.exe"; then
1616
let exe_count=exe_count+1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34808 for x64
1+
Microsoft (R) C/C++ Optimizing Compiler Version 19.50.35721 for x64
22
Copyright (C) Microsoft Corporation. All rights reserved.
33

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

0 commit comments

Comments
 (0)