且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

注释会翻译成机器代码吗? C ++

更新时间:2022-06-10 17:00:58

注释通常在预处理过程中被剥离,因此编译器本身根本看不到它们。

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.