如何在C ++模块系统中处理模板? [英] How are templates handled in C++ module system?

查看:78
本文介绍了如何在C ++模块系统中处理模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 C ++的模块系统
可以理解C ++模块,这是C ++的一项建议功能。

I am reading the paper A Module System for C++ to understand C++ modules, a proposed feature for C++.

我无法完全理解

有什么想法吗?

推荐答案

当前C ++实现确实只有两个与代码相对应的事物:我们人工编写和编辑的源代码以及汇编,编译器根据源代码吐出它们。

Currently C++ implementations really only have two "things" that correspond to code: source code that we human write and edit, and assembly, which the compiler spits out based on source.

因为C ++模板是 reified的,所以每个模板实例化都需要单独进行汇编。因此,无法在定义模板的地方组装,而只能在使用模板的地方组装。这就是为什么模板必须位于头文件中以便基本上可以将它们复制粘贴到使用点的原因(这就是#include确实如此)。

Because C++ templates are "reified", separate assembly is spit out for each template instantiation. For that reason, no assembly can be produced where the templates are defined, but only where they are used. Which is why templates have to be in header files so they can basically be copy pasted into the point of use (that's all #include is really).

具有代码的第三种表示形式。想象一下,在内部,编译器在解析代码之后在之后具有某种内部表示形式,但是在开始生成程序集的之前。它产生的事物最终是抽象语法树(AST)的某种表示形式。基本上就是您的程序,从对人类最简单的形式映射到对计算机最简单的形式。

The idea is to have a third representation of the code. Imagine that internally the compiler has some kind of internal representation after it has parsed the code but before it starts producing assembly. The "thing" it produces is ultimately some kind of representation of an abstract syntax tree (AST). It's basically exactly your program, mapped from a form that is easiest for humans, to a form that is easiest for computers.

这大致是模块(或至少是它们的实现)。您将您的代码,并吐出某种表示AST的文件。此AST可以完全代表您的程序,因此完全无损。它了解有关您声明的模板的所有信息,依此类推。加载模块时,它只会加载该文件,编译器可以完全像使用所有可用源一样使用它。但是,将人类可读源转换为AST的步骤实际上是相当昂贵的步骤。从AST开始的速度可能要快得多。

This is very roughly the idea behind modules (or at least their implementation). You take your code, and spit out some kind of file representing the AST. This AST is a full representation of your program, so it's completely lossless. It knows everything about the templates you declared, and so on. When a module is loaded, it would just load this file and the compiler can use it exactly as if it had all the source available. But, the step of turning human readable source into this AST is actually quite an expensive step. Starting with the AST can be a lot faster.

如果您只有一个翻译单位,那会比较慢。毕竟,解析->代码生成仍然比解析->序列化->反序列化->代码生成更快。但是,假设您有10个翻译单元,它们全部包含#include向量。您将在向量中解析代码10次。此时,您只需解析一次即可抵消序列化/反序列化的额外成本(而且反序列化的速度比解析要快得多;此数据格式将专门设计用于加快反序列化的速度,而源代码是

If you only have one translation unit, this would be slower. After all, parsing -> codegen is still faster than parsing -> serialize -> deserialize -> codegen. But say you have 10 translation units that all #include vector. You will parse the code in vector 10 times. At this point, the extra cost of serializing/deserializing is offset by the fact that you only have to parse once (and deserializing can be made much faster than parsing; this data format will be designed specifically to make deserializing fast, whereas source code is designed to be readable, backwards compatible, etc).

从某种意义上说,预编译的标头是模块的先行预览: https://clang.llvm.org/docs/PCHInternals.html

Pre compiled headers in some sense are a sneak preview of modules: https://clang.llvm.org/docs/PCHInternals.html

这篇关于如何在C ++模块系统中处理模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆