"The Year Of C++ Successor Languages" [2022], Lucian Radu Teodorescu (https://accu.org/journals/overload/30/172/teodorescu/).
Via HN: https://news.ycombinator.com/item?id=34216159
#Cpp #CPlusPlus #Carbon #Val #Cpp2 #ProgrammingLanguage #Successor
#cpp #cplusplus #carbon #val #cpp2 #programminglanguage #successor
@luci #cpp #cpp2 #cplusplus
Timur Doumler
@timur_audio
The Cpp2 compiler, called Cppfront, is not a full compiler, but actually translates Cpp2 code into Cpp1 (today's C++) which can in turn be consumed by any conforming C++ compiler. The C++ output is tuned to be human-readable.
1/13
New syntax, for example
auto main() -> int { ... }
becomes
main: () -> int = { ... }
Cpp2 grammar is context-free, you never need to do sema in order to parse Cpp2 code. Lots of syntax cleanups but overall syntax doesn't look wildly different (unlike Carbon).
2/13
The different grammar makes it easy for the compiler to distinguish Cpp2 code from Cpp1 code. The compiler can compile either pure Cpp2 code or you can have Cpp2/Cpp1 mixed mode in the same file. In pure Cpp2 mode, you can't have headers (use modules), there are no macros.
3/13
Cpp2 gets the defaults right, for example any function is [[nodiscard]] by default. Pattern matching is built in (using his "is" and "as" syntax from http://wg21.link/p2392)
4/13
wg21.link
P2392R1: Pattern matching using "is" and "as"
Lambda syntax is a lot shorter and looks pretty much like regular functions but unnamed. No capture clause, instead you just capture in the body of the lambda, using a $ postfix. Lambdas always capture by value. There is also string interpolation (also using a $ character). 5/13
A function can now return multiple return values like this:
f: () -> (i: int, s: std::string)
which gets translated to returning an instance of an unnamed struct with two members that can bind to a structured binding at the call site.
6/13
Cpp2 is a safe language. Pointer arithmetic doesn't exist. You use span, which is bounds-checked. There are no owning raw pointers, no naked new/delete, therefore also no memory leaks/dangling pointers.
7/13
Instead of naked new, you get shared.new and unique.new, which returns a shared_ptr and unique_ptr, respectively. There is a plain "new" which by default returns a unique_ptr.
8/13
Type safety: he replaced all C and C++ casts with the "as" keyword, which eliminates all unsafe casts. No unions, only variant. Cpp2 has contracts: pre and post conditions and assertions.
9/13
Interop with C++ is seamless. You can "just" use C++ in Cpp2, including the standard library. No thunking, no wrapping, no marshalling, you get direct calls. There is an option to inject bounds checks into all containers. Cpp2 itself has bounds safety built-in.
10/13
Lifetime safety. Static lifetime analysis not yet fully implemented, but pointers can't be null in Cpp2. Initialisation safety: you can have an uninitialised variable but you cannot use it. Static analysis of definite first/last use of a variable. It's composable too.
11/13
All types (including pointers, memory buffers, etc) follow the same initialisation rules. You don't need std::move and std::forward anymore. Move is automatic on definite last use.
12/13
Reflection, generation, metaclasses, and zero-overhead exceptions are coming in a future version. @mattgodbolt
already added Cpp2 support in @CompileExplore
. And finally, here is Herb's Cpp2/Cppfront GitHub repo with more info: https://github.com/hsutter/cppfront
13/13