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
Cppfront is an experimental compiler from a potential C++ 'syntax 2' (Cpp2) to today's 'syntax 1' (Cpp1), to learn some things, prove out some concepts, and share some ideas. This compiler is a work in progress and currently hilariously incomplete... basic functions work, classes will be next, then metaclasses and lightweight exceptions.
10
+
Cppfront is an experimental compiler from a potential C++ 'syntax 2' (Cpp2) to today's 'syntax 1' (Cpp1), to learn some things, prove out some concepts, and share some ideas.
11
11
12
-
-[Goals and history](#goals-and-history)
13
-
-[What's different about this experiment?](#whats-different-about-this-experiment)
14
-
-[How do I build cppfront?](#how-do-i-build-cppfront)
15
-
-[How do I build my `.cpp2` file?](#how-do-i-build-my-cpp2-file)
16
-
-[Where's the documentation?](#wheres-the-documentation)
17
-
-[Papers and talks derived from this work (presented in today's syntax)](#papers-and-talks-derived-from-this-work-presented-in-current-syntax)
## Documentation is [available here](https://hsutter.github.io/cppfront/)
19
13
20
-
## Goals and history
14
+
## What's different about this project?
21
15
22
-
<imagealign="right"width="320"src="https://user-images.githubusercontent.com/1801526/189241726-db92ae64-2b2f-4463-a0c3-87794062da9c.png"> My goal is to explore whether there's a way we can evolve C++ itself to become 10x simpler, safer, and more toolable. If we had an alternate C++ syntax, it would give us a "bubble of new code that doesn't exist today" where we could make arbitrary improvements (e.g., change defaults, remove unsafe parts, make the language context-free and order-independent, and generally apply 30 years' worth of learnings), free of backward source compatibility constraints.
16
+
In short, it aims to help evolve C++ itself, not to be a "C++ successor."
23
17
24
-
In 2015-16 I did most of the 'syntax 2' design work. Since then, my ISO C++ evolution proposals and conference talks have come from this work (see list below) — each presenting one part of the design as a standalone proposal under today's syntax, usually with a standalone prototype implementation, to validate and refine that part. Since 2021, I've been writing this cppfront compiler to prototype all the parts together as a whole as originally intended, now including the alternative 'syntax 2' for C++ that enables their full designs including otherwise-breaking changes.
18
+
Elaborating:
25
19
26
-
## What's different about this experiment?
20
+
**What it isn't.** Cpp2 is not a successor or alternate language with its own divergent or incompatible ecosystem. For example, it does not have its own nonstandard incompatible modules/concepts/etc. that compete with the Standard C++ features; and it does not replace your Standard C++ compiler and other tools.
27
21
28
-
This is one of many experiments going on across the industry looking at ways to accomplish a major C++ evolution. I look forward to seeing how all the experiments work out. This experiment is different in two main ways.
22
+
**What it is.** Cpp2 aims to be another "skin" for C++ itself, just a simpler and safer way to write ordinary C++ types/functions/objects. It seamlessly uses Standard C++ modules and concepts requirements and other features, and it works with all existing C++20 or higher compilers and tools right out of the box with zero overhead.
29
23
30
-
### 1) This is about C++20/23/... — not about something else
24
+
For more, see [What is Cpp2?](https://hsutter.github.io/cppfront/#what-is-cpp2).
31
25
32
-
<imagealign="right"width="150"src="https://user-images.githubusercontent.com/1801526/188887745-23e0c3a0-3ea7-4589-993c-f54fe662b107.png"> For me, ISO C++ is the best tool in the world today to write the programs I want and need. I want to keep writing code in C++... just "nicer":
33
-
34
-
- with less complexity to remember;
35
-
36
-
- with fewer safety gotchas; and
37
-
38
-
- with the same level of tool support other languages enjoy.
39
-
40
-
We've been improving C++'s safety and ergonomics with each ISO C++ release, but they have been "10%" improvements. We haven't been able to do a **"10x"** improvement primarily because we have to keep 100% syntax backward compatibility. So, what if we could have our compatibility cake, and eat it too — by having:
41
-
42
-
- 100% seamless **link compatibility always** (no marshaling, no thunks, no wrappers, no generated 'compatibility modules' to import/export C++ code from/to a different world); and
43
-
44
-
- 100% seamless **backward source compatibility always _available_**, including 100% SFINAE and macro compatibility, but only pay for it when we use it... that is, apply C++'s familiar "zero-overhead principle" also to backward source compatibility?
45
-
46
-
In cppfront this means you have two options always available: (1) _Write mixed Cpp1/Cpp2 in the same source file_ with perfect backward source compatibility via `#include` or `import`. (2) _Write only Cpp2 in a particular source file_ and program in a 10x simpler C++, where code is type-safe and memory-safe by construction, keeps perfect backward link compatibility via `import`, and in the future (if this project succeeds) with faster compilers and better tools tuned for the simpler language.
47
-
48
-
I hope to start a conversation about what could be possible _**within C++**_’s own evolution to rejuvenate C++, now that we have C++20 and soon C++23 to build upon. I want to encourage us to look for ways to push the boundaries to bring C++ itself forward and double down on C++ — not to switch to something else. I want us to aim for major C++ evolution directed toward things that will make us better C++ programmers — not programmers of something else.
49
-
50
-
### 2) This is about measurable improvements to safety, simplicity, and toolability — not about green-field design or random drive-by changes
51
-
52
-
<imagealign="right"width="320"src="https://user-images.githubusercontent.com/1801526/189236486-0b5a4892-42c8-4486-9722-bb60a5df8c7e.png"> My specific goal is to explore the question: Can we make C++ **10x safer, simpler, and more toolable?** I want each proposed improvement to address those known C++ pain points in a measurable way (e.g., reduce a class of CVEs (vulnerabilities) by A%, reduce the guidance we have to teach by B%).
53
-
54
-
An alternative syntax would let us do things we can never do within today's syntax without breaking the world:
55
-
56
-
- fix defaults (e.g., make `[[nodiscard]]` the default);
57
-
- double down on modern C++ (e.g., make C++20 modules and C++23 `import std;` the default);
58
-
- remove unsafe parts that are already superseded (e.g., remove `union` and pointer arithmetic);
59
-
- have type and memory safety by default (e.g., make the [C++ Core Guidelines safety profiles](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-profile) the default and required);
60
-
- eliminate 90% of the guidance we have to teach about today's complex language;
61
-
- make it easy to write a parser (e.g., have a context-free grammar); and
62
-
- make it easy to write refactoring and other tools (e.g., have order-independent semantics).
63
-
64
-
### Acknowledgments
26
+
## Acknowledgments
65
27
66
28
Scores of people have given valuable feedback and many are listed below, but I especially want to thank Joe Duffy, Anders Hejlsberg, Bjarne Stroustrup, Andrew Sutton, Tim Sweeney, and Mads Torgersen for their insights and valuable feedback on this work over the years — especially when they disagreed with me. I'd also like to thank Dave Abrahams, Andrei Alexandrescu, Walter Bright, Lee Howes, Chris McKinsey, Scott Meyers, Gor Nishanov, Andrew Pardoe, Sean Parent, Jared Parsons, David Sankel, Nathan Sidwell, JC van Winkel, and Ville Voutilainen for broad feedback on the design.
67
29
68
30
Many more people are listed below for their help with specific parts of the design and those proposals/prototypes. I apologize for the names I have forgotten.
Run `cppfront your.cpp2`, then run the generated `your.cpp` through any major C++20 compiler after putting `/cppfront/include` in the path so it can find `cpp2util.h`.
90
-
91
-
- MSVC would be: `cl your.cpp -std:c++20 -EHsc`
92
-
- GCC would be: `g++-10 your.cpp -std=c++20`
93
-
- Clang would be: `clang++-12 your.cpp -std=c++20`
94
-
95
-
## Where's the documentation?
96
-
97
-
I'm not posting much documentation because that would imply this project is intended for others to use — if it someday becomes ready for that, I'll post more docs.
32
+
## Further information
98
33
99
34
To learn more, please see:
100
35
101
36
-[**My CppCon 2022 talk, "Can C++ be 10x simpler and safer ...?"**](https://www.youtube.com/watch?v=ELeZAKCN4tY)
102
37
-[**My CppCon 2023 talk, "Cooperative C++ Evolution: Toward a TypeScript for C++"**](https://www.youtube.com/watch?v=8U3hl8XMm8c)
103
-
-**The [cppfront regression tests](https://github.com/hsutter/cppfront/tree/main/regression-tests)** which show dozens of working examples, each with a`.cpp2` file. In the `/test-results/` subdirectory you can see the `.cpp` file that each is translated to. Each filename briefly describes the language features the test demonstrates (e.g., contracts, parameter passing, bounds safety, type-safe `is` queries and `as` casts, initialization safety, and generalized value capture including in function expressions ('lambdas'), postconditions, and string interpolation).
104
-
- The list of papers and talks below.
38
+
-This repo's wiki
39
+
- The list of papers and talks below
105
40
106
-
## Papers and talks derived from this work (presented in current syntax)
41
+
###Papers and talks derived from this work (presented in current syntax as contributions toward ISO C++'s evolution itself)
107
42
108
43
Here are the ISO C++ papers and CppCon conference talks I've given since 2015 that have been derived from this work, in the order that I brought each piece forward. Most of the details in the materials below are still current with only incremental updates, apart from the specific syntax of course.
109
44
110
-
### 2015: Lifetime safety
45
+
####2015: Lifetime safety
111
46
112
47
-[**CppCon 2015**: "Writing good C++14... _by default_"](https://youtu.be/hEx5DNLWGgA) particularly [from 29:00 onward](https://youtu.be/hEx5DNLWGgA?t=1757) shows the Lifetime analysis with live demos in a Visual Studio prototype.
113
48
-[**CppCon 2018**: "Thoughts on a more powerful _and_ simpler C++ (#5 of N)](https://youtu.be/80BZxujhY38):
@@ -118,7 +53,7 @@ Here are the ISO C++ papers and CppCon conference talks I've given since 2015 th
118
53
119
54
This is not yet implemented in cppfront. Implementations are shipping in Visual Studio and in CLion, and initial parts have been upstreamed in Clang. I want to especially thank Matthias Gehre, Gabor Horvath, Neil MacIntosh, and Kyle Reed for their help in implementing the Lifetime static analysis design in Visual Studio and a Clang fork. Thanks also to the following for their input and feedback on the specification: Andrei Alexandrescu, Steve Carroll, Pavel Curtis, Gabriel Dos Reis, Joe Duffy, Daniel Frampton, Anna Gringauze, Chris Hawblitzel, Nicolai Josuttis, Ellie Kornstaedt, Aaron Lahman, Ryan McDougall, Nathan Myers, Gor Nishanov, Andrew Pardoe, Jared Parsons, Dave Sielaff, Richard Smith, Jim Springfield, and Bjarne Stroustrup.
120
55
121
-
### 2016: Garbage-collected memory arena
56
+
####2016: Garbage-collected memory arena
122
57
123
58
-[**CppCon 2016**: "Leak-freedom in C++... _by default_"](https://www.youtube.com/watch?v=JfmTagWcqoE) particularly [from 59:00 onward](https://youtu.be/JfmTagWcqoE?t=3558) where I show the strawman prototype I wrote of a tracing garbage-collection memory arena.
124
59
-[**Github.com/hsutter/gcpp**: "gcpp: Deferred and unordered destruction"](https://github.com/hsutter/gcpp) is the GitHub prototype I wrote.
@@ -130,9 +65,9 @@ This is not yet implemented in cppfront. I welcome a real GC expert to collabora
130
65
-[**CppCon 2017 (just the intro, first 6 minutes)**: "Meta: Thoughts on generative C++"](https://www.youtube.com/watch?v=4AfRAVcThyA).
131
66
-[**P0515**: Consistent comparison](https://wg21.link/p0515) is the proposal in today's syntax that I proposed, and was adopted, for C++20.
132
67
133
-
This is part of ISO C++20 and C++23. Thank you again to everyone who helped land this in the Standard in C++20 and improve it in C++23, including especially Walter Brown, Lawrence Crowl, Cameron DaCamara, Gabriel Dos Reis, Jens Maurer, Barry Revzin, Richard Smith, and David Stone.
68
+
This is part of ISO C++20 and C++23, except only for chained comparisons which is implemented in cppfront. Thank you again to everyone who helped land this in the Standard in C++20 and improve it in C++23, including especially Walter Brown, Lawrence Crowl, Cameron DaCamara, Gabriel Dos Reis, Jens Maurer, Barry Revzin, Richard Smith, and David Stone.
134
69
135
-
### 2017: Reflection, generation, and metaclasses
70
+
####2017: Reflection, generation, and metaclasses
136
71
137
72
-[**ACCU 2017**: "Thoughts on metaclasses"](https://www.youtube.com/watch?v=6nsyX37nsRs) is the first talk I gave about this.
138
73
-[**CppCon 2017**: "Meta: Thoughts on generative C++"](https://www.youtube.com/watch?v=4AfRAVcThyA) from after the intro, [from 6:00 onward](https://youtu.be/4AfRAVcThyA?t=393).
@@ -141,19 +76,19 @@ This is part of ISO C++20 and C++23. Thank you again to everyone who helped land
141
76
- (repeating the Lifetime section bullet above) [The final part starting at 1:28](https://youtu.be/80BZxujhY38?t=5307) shows the Lifetime and Metaclasses proposals working hand-in-hand. This is one of the few places before cppfront where the same compiler has contained prototypes of multiple 'syntax 2'-derived features so I could show how they build on each other when used together.
142
77
-[**P0707**: Metaclass functions: Generative C++](https://wg21.link/p0707) is the paper I brought to the ISO C++ committee.
143
78
144
-
This is not yet implemented in cppfront. Thanks again to Andrew Sutton and his colleagues Wyatt Childers and Jennifer Yao for their help in implementing the Clang-based prototypes of this proposal, and everyone else who contributed feedback on the design including Louis Brandy, Chandler Carruth, Casey Carter, Matúš Chochlík, Lawrence Crowl, Pavel Curtis, Louis Dionne, Gabriel Dos Reis, Joe Duffy, Kenny Kerr, Nicolai Josuttis, Aaron Lahman, Scott Meyers, Axel Naumann, Gor Nishanov, Stephan T. Lavavej, Andrew Pardoe, Sean Parent, Jared Parsons, David Sankel, Richard Smith, Jeff Snyder, Mike Spertus, Mads Torgersen, Daveed Vandevoorde, Tony Van Eerd, JC van Winkel, Ville Voutilainen, and Titus Winters, and many more WG 21 / SG 7 participants.
79
+
This is mostly implemented in cppfront, except for the ability to write your own metafunctions (that's coming). Thanks again to Andrew Sutton and his colleagues Wyatt Childers and Jennifer Yao for their help in implementing the Clang-based prototypes of this proposal, and everyone else who contributed feedback on the design including Louis Brandy, Chandler Carruth, Casey Carter, Matúš Chochlík, Lawrence Crowl, Pavel Curtis, Louis Dionne, Gabriel Dos Reis, Joe Duffy, Kenny Kerr, Nicolai Josuttis, Aaron Lahman, Scott Meyers, Axel Naumann, Gor Nishanov, Stephan T. Lavavej, Andrew Pardoe, Sean Parent, Jared Parsons, David Sankel, Richard Smith, Jeff Snyder, Mike Spertus, Mads Torgersen, Daveed Vandevoorde, Tony Van Eerd, JC van Winkel, Ville Voutilainen, and Titus Winters, and many more WG 21 / SG 7 participants.
145
80
146
-
### 2018: Updates to Lifetime and Metaclasses (see above)
81
+
####2018: Updates to Lifetime and Metaclasses (see above)
-[**ACCU 2019**: "De-fragmenting C++: Making exceptions more affordable and usable](https://www.youtube.com/watch?v=os7cqJ5qlzo).
151
86
-[**CppCon 2019**: "De-fragmenting C++: Making exceptions and RTTI more affordable and usable ("Simplifying C++" #6 of N)](https://www.youtube.com/watch?v=ARYP83yNAWk).
-**ACCU autumn 2019**: "Quantifying accidental complexity: An empirical look at teaching and using C++" was my first public talk about this, but a "beta" version that was not recorded; you can find the description [here](https://accu.org/conf-previous/2019_autumn/sessions/#XQuantifyingAccidentalComplexityAnEmpiricalLookatTeachingandUsingC).
159
94
-[**CppCon 2020**: "Quantifying accidental complexity: An empirical look at teaching and using C++"](https://www.youtube.com/watch?v=6lurOCdaj0Y):
@@ -163,9 +98,9 @@ This is not yet implemented in cppfront.
163
98
-[**Github.com/hsutter/708**](https://github.com/hsutter/708) is a repo with the paper and demo examples as used in the talk.
164
99
-[**P2064**: "Assumptions"](https://wg21.link/p2064) is also related to this 'syntax 2' work, because this work includes a contracts design, and assumptions ought to be separate from that. This paper was making the technical argument why assumptions and assertions (contracts) are different things.
165
100
166
-
This is implemented in cppfront, except not the unified `operator=`experiment which will come with classes. Thanks to Andrew Sutton for an initial Clang-based implementation.
101
+
This is implemented in cppfront, including the unified `operator=`for user-defined types. Thanks to Andrew Sutton for an initial Clang-based implementation.
167
102
168
-
### 2020: "Bridge to NewThingia"
103
+
####2020: "Bridge to NewThingia"
169
104
170
105
In 2020 I also started socializing the ideas of:
171
106
@@ -177,14 +112,14 @@ I had specifically in mind a major C++ evolution's success when many attempts to
177
112
-[**DevAroundTheSun**: "Bridge to Newthingia"](https://herbsutter.com/2020/06/14/talk-video-available-bridge-to-newthingia-devaroundthesun/), an initial 26-minute version.
178
113
-[**C++ on Sea**: "Bridge to NewThingia"](https://www.youtube.com/watch?v=BF3qw1ObUyo) which especially [at the end starting near 48:00](https://youtu.be/BF3qw1ObUyo?t=2883) had a slide that directly tackled the "C++ major evolution" scenario, and laid out what I think it would take to have credible answers to the key questions.
179
114
180
-
### 2021: `is`, `as`, and pattern matching
115
+
####2021: `is`, `as`, and pattern matching
181
116
182
117
-[**CppCon 2021**: "Extending and simplifying C++: Thoughts on pattern matching using `is` and `as`"](https://www.youtube.com/watch?v=raB_289NxBk).
183
118
-[**P2392**: Pattern matching using `is` and `as`](https://wg21.link/p2392) is the ISO C++ committee paper.
184
119
185
-
This is partly implemented in cppfront. There is basic support for `is` and `as`, and very basic `inspect` expressions.
120
+
This is mostly implemented in cppfront. There is support for `is`, `as`, and basic `inspect` expressions.
186
121
187
-
### 2022: CppCon 2022 talk and cppfront
122
+
####2022: CppCon 2022 talk and cppfront
188
123
189
124
-[**CppCon 2022: "Can C++ be 10x simpler and safer ...?"**](https://www.youtube.com/watch?v=ELeZAKCN4tY)
0 commit comments