当前位置: 首页 > 其它资源 > 正文
Google 的 C++ 实验继任者 Carbon 已经来了!

Google 的 C++ 实验继任者 Carbon 已经来了!

作者:大眼仔~旭 日期:2年前 (2022-07-26) 评论:0 条

摘要:自诞生以来,C++ 一直是构建性能密集型应用程序的首选。 但是该语言仍然存在一些由于其“委员会设计”而导致的过时做法。2022 年 7 月 19 日,在多伦多举行的 CPP North C++ 会议期间,Google 工程师 Chandler Carruth 介绍了 Carbon。同时, 在大会中明确表示 Carbon…

自诞生以来,C++ 一直是构建性能密集型应用程序的首选。 但是该语言仍然存在一些由于其“委员会设计”而导致的过时做法。2022 年 7 月 19 日,在多伦多举行的 CPP North C++ 会议期间,Google 工程师 Chandler Carruth 介绍了 Carbon。同时, 在大会中明确表示 Carbon 能够更好的继任 C++。

今天就和大眼仔一起了解 Carbon 是什么以及它打算如何继任 C++。

什么是 Carbon?

Google 工程师开发了 Carbon 编程语言来解决 C++ 现有的缺点。

许多现有的语言(如 Golang 和 Rust)已经存在,它们反映了 C++ 的性能,但是并没有缺点。 不幸的是,这些语言对现有 C++ 代码库的迁移构成了重大障碍。

Carbon 旨在成为 TypeScript 之于 JavaScript,Kotlin 之于 Java。 它不是替代品,而是围绕与 C++ 的互操作性而设计的后继语言。 它旨在为现有代码库和开发人员大规模采用和迁移。

Carbon 的主要功能

Carbon 的一些关键特性包括 C++ 互操作性、现代泛型和内存安全。

与 C++ 的互操作性
Carbon 旨在为 C++ 开发人员提供一个温和的学习曲线,并提供一套标准、一致的语言结构。

例如,以这个 C++ 代码为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// C++:
#include <math.h>
#include <iostream>
#include <span>
#include <vector>
 
struct Circle {
  float r;
};
 
void PrintTotalArea(std::span<Circle> circles) {
  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<Circle> circles = {{1.0}, {2.0}};
 
  // Implicitly constructs `span` from `vector`.
  PrintTotalArea(circles);
  return 0;
}

转换为 Carbon,它变为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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 代码。 例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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 <vector>
#include "circle.h"
#include "geometry.carbon.h"
 
auto main(int argc, char** argv) ->; int {
  std::vector<Circle> circles = {{1.0}, {2.0}};
 
  // Carbon's `Slice` supports implicit construction from `std::vector`,
  // similar to `std::span`.
  Geometry::PrintTotalArea(circles);
  return 0;
}

现代泛型系统
Carbon 提供了一个带有检查定义的现代泛型系统。 但它仍然支持可选模板以实现无缝 C++ 互操作性。

这个泛型系统为 C++ 模板提供了很多优势:

  • 通用定义的类型检查。 这避免了为每个实例重新检查定义的编译时间成本。
  • 强大、经过检查的接口。 这些减少了对实现细节的意外依赖,并创建了更明确的合同。

内存安全
Carbon 试图通过以下方式解决内存安全问题,这是困扰 C++ 的一个关键问题:

  • 更好地跟踪未初始化的状态,增加初始化的执行,并加强初始化错误。
  • 设计基本 API 和习惯用法以支持调试和强化构建中的动态边界检查。
  • 具有比 C++ 现有构建模式更全面的默认调试构建模式。

开始使用 Carbon

您现在可以通过查看代码库并使用 Carbon explorer 来探索 Carbon:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Install bazelisk using Homebrew.
$ brew install bazelisk
 
# Install Clang/LLVM using Homebrew.
# Many Clang/LLVM releases aren't built with options we rely on.
$ brew install llvm
$ export PATH="$(brew --prefix llvm)/bin:${PATH}"
 
# Download Carbon's code.
$ git clone https://github.com/carbon-language/carbon-lang
$ cd carbon-lang
 
# Build and run the explorer.
$ bazel run //explorer -- ./explorer/testdata/print/format_only.carbon

Carbon 的路线图

根据 Carbon 路线图,Google 将在 2022 年底之前发布核心工作版本(0.1)来公开实验。他们计划在 2023 年发布 0.2 版本,并在 2024-2025 年发布完整的 1.0 版本。

谷歌是否能够复制他们的其他语言 Golang 和 Kotlin 的成功,还有待观察。

声明:大眼仔旭 | 本文采用署名-非商业性使用-相同方式共享 4.0 国际许可协议[CC BY-NC-SA]进行授权
文章名称:《Google 的 C++ 实验继任者 Carbon 已经来了!
文章固定链接:http://www.dayanzai.me/google-carbon.html
本站资源仅供个人学习交流,请于下载后 24 小时内删除,不允许用于商业用途,否则法律问题自行承担。
转载声明
全部评论: (0条)
^_^ 暂无评论!

发表评论

返回顶部