c++ - 预处理器如何处理宏?

标签 c++ performance macros

我正在阅读 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/

相关文章:

c++ - std::future no_state 异常与断言

c++ - OpenGL透明度是不透明的

c++ - 使用boost将多个文件解压缩到一个文件中

c++ - 如果对象父对象是窗口,Qt 程序运行缓慢

mysql - 将一个大表拆分为多个表

macros - 定义匹配扩展器

c++ - 创建结构类型的变量时预期出现 ';' 错误

javascript - 如何获得 google recaptcha V3 响应

c - WinMain() 到 main() 宏中的奇怪错误

macros - 迭代创建局部变量?