Skip to content

Commit 68b431b

Browse files
committed
Test to try out Pages
1 parent 56f550d commit 68b431b

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/index.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
# A tour of Cpp2 ('C++ alt syntax 2') and the `cppfront` compiler
3+
4+
## Preface: What is this?
5+
6+
### Goal in a nutshell: 100% pure C++... just nicer
7+
8+
My goal for this project is to try to prove that Bjarne Stroustrup has long been right: That it's possible and desirable to have true C++ with all its expressive power and control and with full backward compatibility, but in a C++ that's **10x simpler** with fewer warts and special cases, and **50x safer** where it's far easier to not write vulnerability bugs by accident.
9+
10+
Stroustrup said it best:
11+
12+
> "Inside C++, there is a much smaller and cleaner language struggling to get out." <br>— Bjarne Stroustrup, _The Design and Evolution of C++_ (D&E), 1994
13+
>
14+
> "Say 10% of the size of C++ in definition and similar in front-end compiler size. ... most of the simplification would come from generalization." <br>— Bjarne Stroustrup, _ACM History of Programming Languages III_, 2007
15+
16+
But how?
17+
18+
### Approach in a nutshell: Alternative syntax + perfect compatibility
19+
20+
This project explores creating an alternate "syntax 2" (Cpp2 for short) for C++ itself, that's unambiguous with today's syntax (Cpp1 for short). That gives us:
21+
22+
- a "bubble of new code" that doesn't exist today, where we can make any change we want in a fully compatible way, without worrying about breaking existing code;
23+
- a way to make any improvement, including to fix language defaults and make all the C++ best-practices guidance we already teach be the default;
24+
- the power to freely use both syntaxes in the same file if we want full backward C++ source compatibility, or to freely use just the simpler syntax standalone if we want to write in a 10x simpler C++ (i.e., pay for source compatibility only if you use it); and
25+
- perfect interoperability, because any type/function/object written in either Cpp1 or Cpp2 syntax is always still just a normal C++ type/function/object.
26+
27+
In the 1980s and 1990s, Stroustrup similarly ensured that C++ could be interleaved with C in the same source file, and C++ could always call any C code with no wrapping/marshaling/thunking. Stroustrup accomplished this and more by writing **cfront**, the original C++ compiler, to translate C++ to pure C. That way, people could start trying out C++ code in any existing C project with just another build step to translate the C++ to C, and the result Just Worked with existing C tools.
28+
29+
This project aims to follow Stroustrup's implementation approach, with a **cppfront** compiler that compiles Cpp2 syntax to Cpp1 syntax. You can start trying out Cpp2 syntax in any existing C++ project just by adding a build step to translate the Cpp2 to Cpp1 syntax, and the result Just Works with existing C++ tools.
30+
31+
What does it look like?
32+
33+
## Hello, world!
34+
35+
Here is the usual starter program that prints "Hello, world!":
36+
37+
```cpp
38+
// hello.cpp2
39+
main: () = {
40+
std::cout << "Hello, world!";
41+
}
42+
```
43+
44+
This is a complete program that prints `Hello, world!`.
45+
46+
Everything in Cpp2 is declared using the syntax **"_name_ `:` _kind_ `=` _statement_"**. The `:` is pronounced "is a." Here, `main` is a function that takes no arguments, and has a body that prints the string to `cout`.
47+
48+
We can just use `std::cout` and `std::operator<<` directly. Cpp2 code works with any C++ code or library, using direct calls without any wrapping/marshaling/thunking.
49+
50+
We didn't need `#include <iostream>` or `import std;`. The full C++ standard library is always available by default if your source file contains only syntax-2 code and you compile with it `cppfront -p` (short for `-pure-cpp2`).
51+
52+
### Building and running the program
53+
54+
Now use `cppfront` to compile `hello.cpp2` to a standard C++ file `hello.cpp`:
55+
56+
```
57+
cppfront hello.cpp2 -p # produces hello.cpp
58+
```
59+
60+
and then build `hello.cpp` using your favorite C++20 compiler, where `CPPFRONT_INCLUDE` is the path to `/cppfront/include`:
61+
62+
```
63+
# --- MSVC -----------------------------------------------
64+
> cl hello.cpp -std:c++20 -EHsc -I CPPFRONT_INCLUDE
65+
> hello.exe
66+
Hello, world!
67+
68+
# --- GCC ------------------------------------------------
69+
$ g++ hello.cpp -std=c++20 -ICPPFRONT_INCLUDE -o hello
70+
$ ./hello.exe
71+
Hello, world!
72+
73+
# --- Clang ----------------------------------------------
74+
$ clang++ hello.cpp -std=c++20 -ICPPFRONT_INCLUDE -o hello
75+
$ ./hello.exe
76+
Hello, world!
77+
```
78+

0 commit comments

Comments
 (0)