如果指令宏比较 [英] If-directive macro comparison

查看:60
本文介绍了如果指令宏比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么满足以下代码中的 #if 条件:

Why is the #if condition in the following code fulfilled:

#include <iostream>
#define VALUE foo    

int main() {    
#if VALUE == bar
    std::cout << "WORKS!" << std::endl;
#endif // VALUE
}

推荐答案

页面上cppreference.com 状态:

所有宏扩展后,对defined和__has_include进行评估(自C ++ 17起)表达式,不是布尔值的任何标识符文字被替换为数字``0''(包括标识符是词汇上的关键字,但不是替代标记,如and).

After all macro expansion and evaluation of defined and __has_include (since C++17) expressions, any identifier which is not a boolean literal is replaced with the number ​0​ (this includes identifiers that are lexically keywords, but not alternative tokens like and).

因此,先将 VALUE 替换为 foo ,然后将 foo bar 都替换为0.

So VALUE is first replaced with foo, and then both foo and bar are replaced with 0.

这篇关于如果指令宏比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆