Cppfront – A new Syntax for CPP

C++ is a good programming language, it is used in performance-critical areas like gaming, multimedia processing, etc but some parts of CPP language syntax are still old-style (cpp1). Cppfront is an experimental compiler for C++ syntax2 (cpp2) by  Herb Sutter.

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.

-Herb Sutter

In Cppfront we have two options :

  • Write mixed Cpp1/Cpp2 in the same source file with perfect backward source compatibility via #include or import
  • 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 Cppfront succeeds) with faster compilers and better tools tuned for the simpler language.

Herb Sutter suggests it would be nice to have the following features in C++

  • less complexity to remember;
  • with fewer safety gotchas; and
  • the same level of tool support that other languages enjoy.

CPP2 syntax focus on the following areas:

  • fix defaults (e.g., make [[nodiscard]] the default);
  • double down on modern C++ (e.g., make C++20 modules and C++23 import std; the default);
  • remove unsafe parts (e.g., remove union and pointer arithmetic);
  • have type and memory safety by default (e.g., make the C++ Core Guidelines safety profiles the default and required);
  • eliminate 90% of the guidance we have to teach about today’s complex language;
  • make it easy to write a parser (e.g., have context-free grammar); and
  • make it easy to write refactoring and other tools (e.g., have order-independent semantics)

Let’s try Cppfront.

Build Cppfront compiler

I used Ubuntu 22.04 first we need to install g++-10

sudo apt-get update
sudo apt install g++-10

Next clone the Cppfront repository.

git clone https://github.com/hsutter/cppfront.git

Enter into the “sources” directory and run the following command to build Cppfront:

g++-10 cppfront.cpp -std=c++20 -o cppfront

It will build the Cppfront executable.

Write some CPP2 code, build and run

Let’s write a hello world program using CPP2 syntax.

Open your favorite text editor and write the following code :

main: () -> int = {
 std::cout << "Hello world CPP2\n"; 

Save it as helloworld.cpp2, then run:

cppfront helloworld.cpp2

It will produce a helloworld.cpp file now we can compile it using the following command(Please note that we need to include cpp2util.h’s path ):

g++-10 helloworld.cpp -std=c++20 -I/home/rupesh/cppfront/include -o helloworldapp

Now run ./helloworld it will print “Hello world CPP2”.

Hello world cpp2 output

One more example in pure cpp2

main: () -> int = {
    std::cout << "Hello " << name() << "\n";

name: () -> std::string = {
    s: std::string = "NoloWiz";
    return s;

decorate: (inout s: std::string) = {
    s = "[" + s + "]";

Compile and run :

./cppfront greetings.cpp2 -p
g++-10 greetings.cpp -std=c++20 -I/home/rupesh/cppfront/include -o greetings
CPP 2 output

More examples are available here.