| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -391,7 +391,7 @@ outer: while i<M next i++ { // loop named "outer" | |||
| 391 | 391 | ||
| 392 | 392 | For example: | |
| 393 | 393 | ||
| 394 | - ``` cpp title="Throw, try, catch" | ||
| 394 | + ``` cpp title="Throw, try, catch" hl_lines="1 3-5 9-11" | ||
| 395 | 395 | // 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 | + ### <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 | + | ||
| 410 | 443 | ## <a id="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. | |
| 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 | + #### <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 | + | ||
| 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.** | |
| 373 | 403 | A simple hello diff example is: | |
| 374 | - ``` | ||
| 404 | + | ||
| 405 | + ``` cpp | ||
| 375 | 406 | ad: @autodiff type = { | |
| 376 | 407 | func: (x: double) -> (r: double) = { | |
| 377 | 408 | r = x * x; | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + 42 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + 42 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,7 @@ for f in *.cpp | |||
| 10 | 10 | do | |
| 11 | 11 | let count=count+1 | |
| 12 | 12 | 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 | ||
| 14 | 14 | rm -f $f | |
| 15 | 15 | if test -f "test.exe"; then | |
| 16 | 16 | let exe_count=exe_count+1 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + 42 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,7 @@ for f in *.cpp | |||
| 10 | 10 | do | |
| 11 | 11 | let count=count+1 | |
| 12 | 12 | 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 | ||
| 14 | 14 | rm -f $f | |
| 15 | 15 | if test -f "test.exe"; then | |
| 16 | 16 | let exe_count=exe_count+1 | |
| Original file line number | Diff line number | Diff 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 | ||
| 2 | 2 | Copyright (C) Microsoft Corporation. All rights reserved. | |
| 3 | 3 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + 42 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments