我当时正在读Scott Meyers写的“Effective c++” 偶然发现了“翻译单元”这个术语。

谁能给我解释一下:

到底是什么? 当我用c++编程时,什么时候应该考虑使用它? 它只与c++相关,还是也可以与其他编程语言一起使用?

我可能已经在不知道术语的情况下使用它了……


当前回答

这本书讲得够清楚了。当Meyers提到“翻译单元”时,他指的是源代码文件。

其他回答

从这里:(时光倒流机链接)

According to standard C++ (wayback machine link) : A translation unit is the basic unit of compilation in C++. It consists of the contents of a single source file, plus the contents of any header files directly or indirectly included by it, minus those lines that were ignored using conditional preprocessing statements. A single translation unit can be compiled into an object file, library, or executable program. The notion of a translation unit is most often mentioned in the contexts of the One Definition Rule, and templates.

翻译单元是传递给编译器的代码。这通常意味着在.c文件上运行预处理器的输出。

根据MSDN: C和c++程序由一个或多个源文件组成,每个源文件包含程序的一些文本。源文件与其包含文件(使用#include预处理器指令包含的文件)一起,但不包括被条件编译指令(如#if)删除的代码部分,称为“翻译单元”。

这本书讲得够清楚了。当Meyers提到“翻译单元”时,他指的是源代码文件。

正如其他人所说,翻译单元基本上是源文件经过预处理后的内容。它是语言语法的最高产物;只有在编写C或c++编译器时才需要担心这个问题。