【发布时间】:2020-11-03 12:11:44
【问题描述】:
这个问题没有实际用处!我问这个只是因为我很好奇!
在 C++ 中有一种方法可以通过在某处写入 #define true false 来将 true 伪造为 false,然后代码中的任何地方 true 都将被视为 false。但我正在寻找一种同时将true 伪造为false 和false 为true 的方法:
#define true false
#define false true
这不起作用,尝试“保存”原始 true 也不起作用:
#define temptrue true
#define true false
#define false temptrue
你知道有什么办法吗?
【问题讨论】:
-
这是未定义的行为。 “它不起作用”是意料之中的。
-
感谢@MSalters。除了我的回答(当然 ;-))这是此页面上唯一理智的评论或回答。
-
@Bathsheba 我和接受的答案 也 指出这是未定义且毫无意义的。
-
不允许从语言中重新定义任何关键字。使程序基本无效。
-
参见章节:
17.6.4.3 Reserved names第 2 段A translation unit shall not #define or #undef names lexically identical to **keywords**, to the identifiers listed in Table 2, or to the attribute-tokens described in 7.6.参见章节2.11 Keywords了解包含真假的关键字。
标签: c++ c-preprocessor