- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
来自 How To Write Unmaintainable Code (结构缩进):
One of the most imaginative uses of the preprocessor I have heard of was requiring five passes through CPP before the code was ready to compile. Through clever use of defines and ifdefs, a master of obfuscation can make header files declare different things depending on how many times they are included. This becomes especially interesting when one header is included in another header. Here is a particularly devious example:
#ifndef DONE
#ifdef TWICE
// put stuff here to declare 3rd time around
void g(char* str);
#define DONE
#else // TWICE
#ifdef ONCE
// put stuff here to declare 2nd time around
void g(void* str);
#define TWICE
#else // ONCE
// put stuff here to declare 1st time around
void g(std::string str);
#define ONCE
#endif // ONCE
#endif // TWICE
#endif // DONE
鉴于此代码,我预计会出现以下行为:
#ifndef DONE
为true
,进入那个 block #ifdef TWICE
为false
#else//TWICE
#ifdef ONCE
为false
#else//ONCE
#define ONCE
#endif
三遍为什么预处理器要进行多次“通过”?预处理器指令中的 if-else 是否表现得不像标准的 if-else 控制结构?在预处理器 if 语句中调用函数是否会导致完全重新评估?
最佳答案
引用你引用的内容:
a master of obfuscation can make header files declare different things depending on how many times they are included
此处的作者假设您多次包含此文件。换句话说,您可能有一个看起来像这样的文件:
#include "that.file.in.your.question.h"
#include "that.file.in.your.question.h"
#include "that.file.in.your.question.h"
第一次,流程如你所描述,导致ONCE
被定义。第二次,因为现在定义了 ONCE
,所以它采用了不同的分支(定义了 TWICE
的分支)。第三次,因为定义了 TWICE
,所以它采用了不同的分支(定义了 DONE
的分支)。最后,第四次,以及此后的任何一次,因为定义了 DONE
,所以跳过了整个过程。当然,除非某些其他预处理器指令取消定义 DONE
。
关于c++ - 预处理器指令 if-elses 是否正常运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20780924/
这个问题在这里已经有了答案: Does an else if statement exist? (2 个答案) 关闭 4 年前。 相信大家在学习C的时候,都学过这样的语法: if (conditio
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Java 11.6 在这个 BMI 计算器中,它将输入人的体重、高度并计算 BMI。 BMI 计算正确,但是在分类 BMI 方法中,程序会跳过 if 和 else if 检查,只打印出每次测试的“肥胖
我有以下代码,它根据 url 参数更改,然后隐藏表单上的选择选项。即 www.example.com?type=images 最终会有 20 多个不同的参数。我想知道比拥有大量 if else 更好的
在WikiPedia Binary Search 的文章中有一个名为Deferred detection of equality 的部分,其中介绍了二进制搜索的某种“优化”版本,如下所示: int b
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在下面的代码中遇到错误。尝试了 if 和 elseif 的所有组合,但无法使其工作。 错误:未捕获的语法错误:意外的标记其他 if(disableAd == 0 && disableWeather
我是 Java 初学者,我正在做一些练习题来提高我的 Java 技能。 编写一个名为 season 的方法,该方法接受两个整数作为代表月份和日期的参数,并返回一个指示该月份和日期的季节的字符串。假设月
有没有可能在 JavaScript 中做这样的事情? if (name == 'foo') { exampleFunction('some_arg'); } else if (name ==
我有一段简单的代码来检查一些条件,我希望每个条件都产生一个唯一的输出,但如果没有满足条件,则产生另一个唯一的输出。 有什么方法可以创建仅在所有先前的 if 语句都失败时才触发的 else 吗?我知道下
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我发现自己经常使用这样的模式: if (a > b) { foo(); } elseif (c > d) { bar(); } else { baz(); } 这里的要点是第二
Hive 使用 IF(条件,表达式,表达式),所以当我想做 if/else if/else if/else 时,我必须这样做: IF(a, 1, IF(b, 2, IF(c, 3, 4))) 有没有更
直到 if 和 else 具有共同的作用域: if (int x = foo()) { // .... } else if (int x = bar()) { // ... } else {
是否可以保证 if-else if-else if-else block 中的 ifs 会按照编写顺序进行测试。 我问这个是因为我经常尝试通过将最常见的情况放在首位来优化我的代码,我想知道编译器所做的
我有一个方法可以检查某些事情并根据这些检查返回一个 bool 值。它涉及一个单独的分支 If 部分,该部分按顺序检查大约 5 个条件。如果这些条件中的任何一个返回真,则该方法将返回真;。如果没有任何条
这个问题在这里已经有了答案: Yet Another Conditional Operator Nesting Question (2 个回答) Why is the conditional oper
有没有一种方法可以从一个事件运行多个 if else 语句?例如,尝试运行此命令,但它仅对类为“two”的元素执行第一个 if else,而不是对类为“三”的第二个元素执行。 $(".hamburge
我有一个类似这样的查询: SELECT SUM(`table`.points) AS total_points FROM table 这将返回类似这样的内容 | **points** |
我正在尝试对单选按钮组运行“存在检查”,以确定如果在组中选择了 2 个单选按钮中的 1 个(if 语句),如果选择了 2 个单选按钮中的另一个(否则 if语句)或者如果两者都没有选择(else 语句)
我是一名优秀的程序员,十分优秀!