我正在阅读 Efficient c++(旧版本)并且有一些疑问。 例如,在这里,它说:
When you do something like this
#define ASPECT_RATIO 1.653
the symbolic name ASPECT_RATIO may never be seen by the compilers; it may be removed by the preprocessors before the source code ever gets compiled. As a results the ASPECT_RATIO may never get entered to SYMBOLIC_TABLE. It an be confusing if you get an error during compilation involving the constant, because the error message may refer to 1.653 and not ASPECT_RATIO
这一段没看懂,怎么能去掉预处理器,就这样。可能的原因是什么,它们在现实世界中的可行性如何。
谢谢
最佳答案
I don't understand this paragraph under inverted quotes.How can anything be removed the preprocessor, just like that. what could be the reasons and how feasible are they in real world.
基本上它描述的正是 C 和 C++ 预处理器的工作原理。原因是用它们的实际值替换宏/常量(使用 #define
指令),而不是一遍又一遍地重复相同的值。在 C++ 中,使用 C 风格的宏被认为是一种不好的风格,但它们支持 C 兼容性。
顾名思义,预处理器在实际编译之前运行,并且基本上按照预处理器指令(以 #
开头的指令)的指示更改源代码。这还包括用它们的值替换宏,按照 #include
指令的指示包含头文件,等等。
这用于避免代码重复、魔数(Magic Number)、共享接口(interface)(头文件)和许多其他有用的东西。
关于c++ - 预处理器如何处理宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830161/