Cpp2 for C++ serves a similar purpose as TypeScript does for JavaScript

TypeScript was developed to address several key challenges faced by developers working with JavaScript, particularly in large-scale applications. Indeed, TypeScript is a superset of JavaScript that introduces optional static typing and other features to enhance the development experience, particularly for large-scale applications. Its history reflects the growing need for better tooling and structure in JavaScript development.

Browsers do not recognize TypeScript directly; therefore, it must be transpiled into valid JavaScript code so that it can be executed by the browsers.

CppFront, created by Herb Sutter, is an experimental tool designed to explore Cpp2, an alternative syntax for C++ that aims to improve the language’s usability, readability, and safety. Much like how TypeScript enhances JavaScript by adding type safety, modern syntax, and error detection, CppFront seeks to bring improvements to C++ while retaining compatibility with existing code and libraries.

And like TypeScript Cpp2 is designed to work by first converting Cpp2 code into standard C++ code before compiling. The tool that makes this possible is called CppFront which is similar to the TS compiler.

Herb Sutter presented Cpp2 two years ago, and still actively working on it:

CppFront is introduced to help developers explore a safer and more expressive way of writing C++ code. Here’s how the process works:

1- Cpp2 Syntax Parsing:

When you write code in Cpp2, it uses a simpler, safer syntax compared to traditional C++. Cpp2 aims to reduce common C++ pitfalls, introducing features like explicit ownership models, safe pointers, and modern control structures. This Cpp2 code still represents C++ constructs but uses a syntax that encourages safer, more maintainable programming.

2- CppFront Translation to C++:

CppFront takes this Cpp2 code and translates it into standard C++ code, which then conforms to C++20 or earlier standards. This conversion involves mapping Cpp2 constructs to their equivalent C++ structures, preserving behavior while improving readability and safety. For example, contracts and pattern matching in Cpp2 are reinterpreted in C++ code so that existing C++ compilers can understand them.

3- Compiling with a Standard C++ Compiler:

Once CppFront has converted the Cpp2 code to C++, you can then compile it using any standard C++ compiler (like GCC, Clang, or MSVC). This ensures compatibility with existing C++ ecosystems, libraries, and tools.

This source-to-source translation approach allows Cpp2 to be backward compatible with C++ while letting developers incrementally adopt safer syntax, much like TypeScript’s design of compiling to JavaScript.

Conclusion:

TypeScript was in experiment stage in 2006 and in 2012 the first version of TypeScript was officially announced by Microsoft. Today, TypeScript is a key player in the JavaScript ecosystem, offering developers powerful tools to create scalable and maintainable applications. Its growth reflects the broader trends in software development, where type safety and developer productivity are increasingly prioritized.

I hope the story of Cpp2 will mirror that of TypeScript, leading to widespread adoption in a few years to simplify and enhance the safety of C++ development.