gpt4 book ai didi

c++ - #define 可以被赋予定义的结果吗?

转载 作者:行者123 更新时间:2023-11-30 19:57:35 24 4
gpt4 key购买 nike

以下代码的结果是用 C 或 C++ 定义的吗?

#define FOO
#define BAR defined(FOO)
#if BAR
int x = 1;
#else
int x = 2;
#endif

最佳答案

使用已定义作为条件指令中的扩展被视为未定义行为

If the token defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined.

None of these macro names, nor the identifier defined, shall be the subject of a #define or a #undef preprocessing directive.

If the defined operator appears as a result of a macro expansion, the C standard says the behavior is undefined. GNU cpp treats it as a genuine defined operator and evaluates it normally. It will warn wherever your code uses this feature if you use the command-line option ‘-pedantic’, since other compilers may handle it differently.

关于c++ - #define 可以被赋予定义的结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41773080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com