[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Aritile/cppfront/main/docs/cpp2/common.md [Back]  [Original]

# Common programming concepts ## `main` As always, `main` is the entry point of the program. For example: `main` can have either: - No parameters:   **`#!cpp main: () /*etc.*/`** - One parameter of implicit type named `args`:   **`#!cpp main: (args) /*etc.*/`** - The type of `args` cannot be explicitly specified. It is always `cpp2::args_t`, which behaves similarly to a `#!cpp const std::array`. - Using `args` performs zero heap allocations. Every `string_view` is directly bound to the string storage provided by host environment. - `args.argc` and `args.argv` additionally provide access to the raw C/C++ `main` parameters. ``` cpp title="main with (args)" hl_lines="5 9" // Print out command line arguments, then invoke // a Qt event loop for a non-UI Qt application main: (args) -> int = { for args do (arg) { std::cout identifier == "void"; // Cpp2 equivalents: address = tokens*[pos + num]&; is_void = u**.identifier* == "void"; ``` Postfix notation lets the code read fluidly left-to-right, in the same order in which the operators will be applied, and lets declaration syntax be consistent with usage syntax. For more details, see [Design note: Postfix operators](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-operators). > Note: The function call syntax `f(x)` calls a namespace-scope function, or a function object, named `f`. The function call syntax `x.f()` is a unified function call syntax (aka UFCS) that calls a type-scope function in the type of `x` if available, otherwise calls the same as `f(x)`. For details, see [Design note: UFCS](https://github.com/hsutter/cppfront/wiki/Design-note%3A-UFCS). | Unary operator | Cpp2 example | Cpp1 equivalent | |---|---|---| | `#!cpp .` | `#!cpp obj.f()` | `#!cpp obj.f()` | | `#!cpp *` | `#!cpp pobj*.f()` | `#!cpp (*pobj).f()` or `#!cpp pobj->f()` | | `#!cpp &` | `#!cpp obj&` | `#!cpp &obj` | | `#!cpp ~` | `#!cpp val~` | `#!cpp ~val` | | `#!cpp ++` | `#!cpp iter++` | `#!cpp ++iter` | | `#!cpp --` | `#!cpp iter--` | `#!cpp --iter` | | `(` `)` | `#!cpp f( 1, 2, 3)` | `#!cpp f( 1, 2, 3)` | | `[` `]` | `#!cpp vec[123]` | `#!cpp vec[123]` | | `$` | `val$` | _reflection no Cpp1 equivalent yet_ | > Because `++` and `--` always have in-place update semantics, we never need to remember "use prefix `++`/`--` unless you need a copy of the old value." If you do need a copy of the old value, just take the copy before calling `++`/`--`. Unary suffix operators must not be preceded by whitespace. When `*`, `&`, and `~` are used as binary operators they must be preceded by whitespace. For example: | Unary postfix operators that
are also binary operators | Cpp2 example | Cpp1 equivalent | |---|---|---| | `#!cpp *` | `#!cpp pobj* * 42` | `#!cpp (*pobj)*42` | | `#!cpp &` | `#!cpp obj& & mask`

(note: allowed in unsafe code only) | `#!cpp &obj & mask` | For more details, see [Design note: Postfix unary operators vs binary operators](https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-unary-operators-vs-binary-operators). ### Binary operators Binary operators are the same as in Cpp1. From highest to lowest precedence: | Binary operators grouped by precedence | |---| | `*`, `/`, `%` | | `+`, `-` | | `` | | `` | | ``, `=` | | `==`, `!=` | | `&` | | `^` | | `|` | | `&&` | | `||` | | `=` and compound assignment | [^u8using]: Or `123.cpp2::u8()` if you aren't `using` the namespace or that specific name.


Web Proxy Viewer  |  New URL  |  Original Page