正在枚举值preprocess时间或在编译时解决? [英] Are enum values resolved in preprocess time or in compile time?
问题描述
当枚举值解决?换句话说,就是下面的code片段符合标准的?
{枚举
一个,
B,
MAX
}#如果MAX> 42
#错误哇!MAX是很多!
#万一
在preprocessor没有什么关系枚举。但是,你的榜样编译没有错误,因此这是怎么回事与#如果MAX> 42
指令?
只要preprocessor正在处理条件指令,也没有定义为宏的标识符为0。对待这样假设 MAX
未在其他地方定义作为一个宏,你的code的片段是等价于:
{枚举
一个,
B,
MAX
}#如果0> 42
#错误哇!MAX是很多!
#万一
从C99 6.10.1 / 3有条件纳入
...一切因宏扩展和替换定义后,
一元运算符已经执行了所有剩余的标识符是
与PP-号0,然后将每个preprocessing令牌是取代
转换成令牌。 ...
块引用>同样的措辞是在C89 / C90标准。
When are enum values resolved? In other words, is the following code snippet standard-compliant?
enum{ A, B, MAX } #if MAX > 42 # error "Woah! MAX is a lot!" #endif
解决方案The preprocessor doesn't have anything to do with enums. But your example compiles without error, so what's going on with the
#if MAX > 42
directive?Whenever the preprocessor is handling a conditional directive, any identifiers that are not defined as macros are treated as 0. So assuming that
MAX
isn't defined elsewhere as a macro, your snippet of code is equivalent to:enum{ A, B, MAX } #if 0 > 42 # error "Woah! MAX is a lot!" #endif
From C99 6.10.1/3 "Conditional inclusion":
... After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers are replaced with the pp-number 0, and then each preprocessing token is converted into a token. ...
The same wording is in the C89/C90 standard.
这篇关于正在枚举值preprocess时间或在编译时解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!