iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔖

C++ Technical Debt and Carbon

に公開
4

Introduction

I welcome corrections or additions through comments or edit requests on GitHub.

Main Subject

Are you familiar with the language called Carbon recently announced by Google?
This language was created to improve upon C++.
However, unlike Rust or Zig, there is no need to rewrite everything.
It is designed (intended to be) to be interoperable, much like TypeScript and JavaScript.
So, why was Carbon necessary?
Will Carbon survive?
By understanding C++, I think the reasons for using Carbon will become clearer.

Things built with or dependent on C++

(Including indirect dependencies)

  • V8
  • Windows
  • Mac
  • Linux
  • Python
  • LLVM
  • Swift
  • Ruby
  • Adobe products
  • Microsoft Office
  • JVM

Take a look.
C++ supports the very foundations of programming.
Humanity depends on C++.

Drawbacks of C++

The syntax is too divergent

C was born about half a century ago. On the other hand, languages other than C that are in use today were born about 10–20 years ago. C++, born in 1983 (39 years ago), has a syntax that is completely different from modern programming languages. For example, here are some programs:

C++
#include <iostream>
int main() {
    int age = 18;
    std::cout << "I am " << age << " years old." << std::endl;
    return 0;
}
Rust
fn main() {
    let age = 18;
    println!("I am {} years old.", age);
}
JavaScript
function main() {
    let age = 18;
    console.log("I am " + age + " years old.");
}
main();
Python
def main():
    age = 18
    print("I am " + age + " years old.")

if __name__ == "__main__":
    main()

Look at C++. It uses some obscure term like std::cout instead of print, printf, or console.log. (Apparently, cout stands for character output.) The syntax is also unique, using << instead of ().

You have to manage memory yourself

Memory management in C/C++ is the programmer's responsibility. While it offers high flexibility because you can manage it yourself, it also increases the number of things you have to consider.

C++
#include <iostream>
int main(int argc, const char **argv)
{
    char *name = new char[32];
    memset(name, 0, 33); // Overflow
    return 0;
}

As shown in the code above, memory operations can lead to unexpected overflows. The compiler does not issue any warnings for the code above. Freedom and responsibility always go hand-in-hand with C/C++.

Not C compatible

One might think that C and C++ are compatible since they can be mixed, but that is not the case. A simple example is auto. In C, auto refers to a local variable, and if the type is omitted, it is interpreted as int. In C++, auto is interpreted through type inference. Therefore, the following code will behave differently in C and C++.

C++
#include <iostream>
int main(int argc, const char **argv)
{
    auto age = 9.5;
    std::cout << "I am " << age * 2 << " years old." << std::endl;
    return 0;
}
C
#include <stdio.h>
int main(int argc, const char **argv)
{
    auto age = 9.5; // Since it is an int type, 0.5 is truncated
    printf("I am %f years old.\n", age * 2); // Result becomes 18
    return 0;
}

Rust or Carbon?

Rust aims to be a replacement for C/C++. It allows you to write memory-safe code easily while maintaining performance nearly identical to C/C++. However, it has a unique and complex syntax, which might be a bit too early for humanity. On the Carbon website, it says:

If you can use Rust, ignore Carbon

So, for the highly skilled individuals who can write Rust, Carbon is unnecessary. Carbon is a language for those who can write C++ but find Rust too difficult.

Summary

I am looking forward to the future growth of Carbon, which is still in its infancy. Personally, I think it will gain massive attention if Carbon is adopted in projects like V8.

GitHubで編集を提案

Discussion

Hidden comment
大森保英大森保英

Linuxは主にC言語で書かれています。少なくとも、kernelはC++に依存していなかったかと。
配布物としては、Qt / KDEを含み、C++に依存することが多いかも知れません。

II

構文がかけ離れすぎてる

C++23以降は std::printstd::println がありますから,標準入出力の1点張りでこの主張を展開するのはやや無理があると思います.

コンパイラは上記コードでは何の警告もしません。

記事執筆時点でリリース済みGCC12では警告されますから,この記述は誤りです.

Riya AmemiyaRiya Amemiya

記事のメンテナンスができておらず申し訳ございません
C++23でC++も進化してたのですね…もはやあの構文は古の構文ということに驚いております

コンパイラの方は当時ローカルで実験してから書いたのですが、執筆時の私の環境のバージョンが古かったのかも知れません

ご指摘いただきました点は修正させていただきます
コメントありがとうございます!