问题描述
当一个用 C++ 编写的程序有注释时,这些注释是被翻译成机器语言还是永远不会那么远?如果我编写了一个 C++ 程序,在两个命令之间添加了整本书的注释,我的程序是否需要更长的时间来编译或运行更慢?
When a program written in C++ has comments, are those comments translated into machine language or do they never get that far? If I write a C++ program with an entire book amount of comments between two commands, will my program take longer to compile or run any slower?
推荐答案
注释通常在预处理期间被删除,因此编译器本身根本看不到它们.
Comments are normally stripped out during preprocessing, so the compiler itself never sees them at all.
它们可以(并且通常会)稍微减慢编译速度——预处理器必须通读整个注释才能找到它的结尾(因此后续代码将传递给编译器.除非您包含真正庞大的注释(例如, 兆字节)但差异可能不会很明显.
They can (and normally do) slow compilation a little though--the preprocessor has to read through the entire comment to find its end (so subsequent code will be passed through to the compiler. Unless you include truly gargantuan comments (e.g., megabytes) the difference probably won't be very noticeable though.
尽管我从未见过(或听说过)这样做的 C 或 C++ 编译器,但已经有编译器(例如,用于 Pascal)使用特殊格式的注释将指令传递给编译器.例如,Turbo Pascal 允许(并且它的后继者可能仍然允许)用户在注释中使用编译器指令打开和关闭范围检查.在这种情况下,注释本身没有(至少在我知道的情况下)没有生成任何机器代码,但它可能并且确实影响了为注释之外的代码生成的机器代码.
Although I've never seen (or heard of) a C or C++ compiler that did it, there have been compilers (e.g., for Pascal) that used specially formatted comments to pass directives to the compiler. For example, Turbo Pascal allowed (and its successor probably still allows) the user to turn range checking on and off using a compiler directive in a comment. In this case, the comment didn't (at least in the cases of which I'm aware) generate any machine code itself, but it could and did affect the machine code that was generated for the code outside the comment.
这篇关于评论会被翻译成机器码吗?C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!