🧵 Is Carbon really the next C++
Tags: computerscience, programming, cpp, discuss
Published: 2022-07-24 16:40:00

The [Carbon Language](https://github.com/carbon-language/carbon-lang) was presented by Google software engineer Chandler Carruth at Toronto’s [C++ North conference](https://cppnorth.ca/index.html) this week. Chandler described the programming language as an “experimental successor to C++“, sparking much interest in the C++ community.

Carbon was launched as a new "experimental" open source programming language to overcome the slow evolution of C++.

Just as Microsoft built Typescript to update JavaScript, and Kotlin was created to shore up weaknesses in Java, Carbon could serve as a successor language to C++, one that offers an easy jumping off point for developers to a newer language that addresses modern development concepts such as memory safety and generics.

{% embed https://twitter.com/code_report/status/1549383435642445824 %}

---

## 🤔 So Why Now ?

Given that C++ is a fork of C and that C is a 50-year-old language, it’s unsurprising that the language has a significant amount of challenges. The Carbon team asserts that, over time, C++ designers added features rather than changing them, leading to complex interactions. The Carbon Language aims to be a clean slate.

C++ is the dominant language for performance-critical software. Carbon’s developers note that incrementally upgrading C++ is exceedingly difficult due to its history and technical debt.

One option is moving to another language, such as Rust, Kotlin, Swift, or Go. But doing so is difficult, and these languages may have a performance cost. The Carbon Language seeks to preserve bidirectional compatibility, match the performance of C++, and have a reasonable learning curve for C++ programmers.

---

## 🤯 Why Carbon

The design wants to release a core working version (“0.1”) by the end of the year. Carbon will be built on a foundation on modern programming principles, including a [generics system](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/overview.md#what-are-generics), that would remove the need to check and recheck the code for each instantiation.

Another much needed feature lacking in C++ is memory safety. Memory access bugs are one of the largest culprits of security exploits. Carbon designers will look for ways to better track uninitialized states, design APIs and idioms that support dynamic bounds checks, and build a comprehensive default debug build mode. Over time, the designers plan to build a safe Carbon subset.

On why a C++ developer might want to consider introducing Carbon to their codebase, Caruth shared some of the language’s highlights on the forum.

- Introducing Keywords and a Simple Grammar
- Function input parameters are read-only values
- Pointers provide indirect access and mutability
- Use expressions to name types
  package root namespace is
- Import APIs via their package name
- Explicit object parameter declares a method
  single inheritance; Classes are final by default
- Powerful, Definition-Checked Generics
- Types explicitly implement interfaces

If you’re interested in getting started with Carbon, you can download the source code and experiment with it on your own device.

{% embed https://github.com/carbon-language/carbon-lang %}

---

## Code Examples

Here is some C++ code translated into Carbon.

First, the C++ code:
![CPP Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qdd7uu83rquua1ilsdb.png)

corresponds to this Carbon code:

![Carbon Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pheuzzkzjv6qquok1sje.png)

You can call Carbon from C++ without overhead and the other way around. This means you migrate a single C++ library to Carbon within an application, or write new Carbon on top of your existing C++ investment. For example:


![Mixed Snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzyj3z89o9c40l03iuea.png)

Beyond interoperability between Carbon and C++, carbon is also planning to support migration tools that will mechanically translate idiomatic C++ code into Carbon code to help you switch an existing C++ codebase to Carbon.

---