DEV Community

Cover image for 🧵 Is Carbon really the next C++
Vedant Chainani
Vedant Chainani

Posted on

🧵 Is Carbon really the next C++

The Carbon Language was presented by Google software engineer Chandler Carruth at Toronto’s C++ North conference 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.

🤔 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, 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.

GitHub logo carbon-language / carbon-lang

Carbon Language's main repository: documents, design, implementation, and related tools. (NOTE: Carbon Language is experimental; see README)

Carbon Language:
An experimental successor to C++

Why? | Goals | Status | Getting started | Join us

See our announcement video from CppNorth. Note that Carbon is not ready for use.

Quicksort code in Carbon. Follow the link to read more.

Fast and works with C++

  • Performance matching C++ using LLVM, with low-level access to bits and addresses
  • Interoperate with your existing C++ code, from inheritance to templates
  • Fast and scalable builds that work with your existing C++ build systems

Modern and evolving

  • Solid language foundations that are easy to learn, especially if you have used C++
  • Easy, tool-based upgrades between Carbon versions
  • Safer fundamentals, and an incremental path towards a memory-safe subset

Welcoming open-source community

  • Clear goals and priorities with robust governance
  • Community that works to be welcoming, inclusive, and friendly
  • Batteries-included approach: compiler, libraries, docs, tools, package manager, and more

Why build Carbon?

C++ remains the dominant programming language for performance-critical software with massive and…

Code Examples

Here is some C++ code translated into Carbon.

First, the C++ code:
CPP Snippet

corresponds to this Carbon code:

Carbon Snippet

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

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.

Top comments (0)