自其诞生以来,得学C++ 一直是实验构建性能密集型应用程序的首选。但由于“委员会式设计 design by committee”,性继习该语言仍有一些过时的否值做法。 2022年7月19日,得学在多伦多举行的实验 CPP North C++ 会议上,Google 工程师 Chandler Carruth 介绍了 Carbon。性继习 我们再来了解什么是否值 Carbon 以及它打算如何取代 C++。 Google 工程师开发了 Carbon 编程语言来解决 C++ 的缺点。 许多现有的实验语言,如 Golang 和 Rust 已经存在,性继习它们反映了 C++ 的否值性能而没有缺点。不幸的得学是,这些语言对现有 C++ 代码库的实验迁移构成了重大障碍。 Carbon 的性继习目标就像 TypeScript 之于 JavaScript, Kotlin 之于 Java。它不是一种替代语言,而是源码下载一种围绕与 C++ 的互操作性而设计的继承性语言。它的目标是对现有代码库和开发人员进行大规模的采用和迁移。 Carbon 的一些关键特性包括 C++ 互操作性、现代泛型和内存安全。 Carbon 旨在为 C++ 开发人员提供一个温和的学习曲线,并提供一套标准、一致的语言结构。 例如,以这个 C++ 代码为例: // C++: #include #include #include #include struct Circle { float r; }; void PrintTotalArea(std::span float area = 0; for (const Circle& c : circles) { area += M_PI * c.r * c.r; } std::cout << "Total area: " << area << endl; } auto main(int argc, char** argv) ->; int { std::vector // Implicitly constructs `span` from `vector`. PrintTotalArea(circles); return 0; 转化为 Carbon,它变成: // Carbon: package Geometry api; import Math; class Circle { var r: f32; } fn PrintTotalArea(circles: Slice(Circle)) { var area: f32 = 0; for (c: Circle in circles) { area += Math.Pi * c.r * c.r; } Print("Total area: { 0}", area); } fn Main() ->; i32 { // A dynamically sized array, like `std::vector`. var circles: Array(Circle) = ({ .r = 1.0}, { .r = 2.0}); // Implicitly constructs `Slice` from `Array`. PrintTotalArea(circles); return 0; 您还可以在应用程序中将单个 C++ 库迁移到 Carbon,或在现有 C++ 代码之上添加新的 Carbon 代码。例如: // C++ code used in both Carbon and C++: struct Circle { float r; }; // Carbon exposing a function for C++: package Geometry api; import Cpp library "circle.h"; import Math; fn PrintTotalArea(circles: Slice(Cpp.Circle)) { var area: f32 = 0; for (c: Cpp.Circle in circles) { area += Math.Pi * c.r * c.r; } Print("Total area: { 0}", area); } // C++ calling Carbon: #include #include "circle.h" #include "geometry.carbon.h" auto main(int argc, char** argv) ->; int { std::vector // Carbons `Slice` supports implicit construction from `std::vector`, // similar to `std::span`. Geometry::PrintTotalArea(circles); return 0; Carbon 提供了一个带有检查定义的现代泛型系统。但它仍然支持可选模板以实现无缝 C++ 互操作性。 这个泛型系统为 C++ 模板提供了很多优势: Carbon 试图通过以下方式解决内存安全问题,这是困扰 C++ 的一个关键问题: 您现在可以通过查看代码库并使用 Carbon explorer 来探索 Carbon: # 使用 Homebrew 安装 bazelisk $ brew install bazelisk # 使用 Homebrew 安装 Clang/LLVM # 许多Clang/LLVM版本并没有使用我们所依赖的选项构建。 $ brew install llvm $ export PATH="$(brew --prefix llvm)/bin:${ PATH}" # 下载 Carbon 代码 $ git clone https://github.com/carbon-language/carbon-lang $ cd carbon-lang # 构建并运行explorer。 根据 Carbon 路线图,Google 将在 2022 年底之前发布核心工作版本(0.1)来公开实验。他们计划在 2023 年发布 0.2 版本,并在 2024-2025 年发布完整的 1.0 版本。 Carbon 是否能够复制 Golang 和 Kotlin 等其他语言的成功,还有待观察。亿华云什么是得学 Carbon?
Carbon 的主要特征
开始使用 Carbon
Carbon 路线图