宏名必须是标识符
Macro name must be an identifier
请告诉我这段代码有什么问题:
// part of main.c
#if have(some_macro)
printf("Import succeeded!\n");
#else
# error Import failed!
#endif
// part of utils.h
#define have(macro) defined(macro)
如果我只写 #if defined(some_macro)
就可以了。
有什么问题?
您不能将 #if
之后的任何内容扩展到 defined
。这受制于未定义的行为。
来自 C99 标准(强调我的):
6.10.1 Conditional inclusion
...
3 Preprocessing directives of the forms
# if *constant-expression new-line group<sub>opt</sub>*
# elif *constant-expression new-line group<sub>opt</sub>*
检查控制常量表达式的计算结果是否为非零。
4 Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined
unary operator), just as in normal text. 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.
请告诉我这段代码有什么问题:
// part of main.c
#if have(some_macro)
printf("Import succeeded!\n");
#else
# error Import failed!
#endif
// part of utils.h
#define have(macro) defined(macro)
如果我只写 #if defined(some_macro)
就可以了。
有什么问题?
您不能将 #if
之后的任何内容扩展到 defined
。这受制于未定义的行为。
来自 C99 标准(强调我的):
6.10.1 Conditional inclusion
...
3 Preprocessing directives of the forms
# if *constant-expression new-line group<sub>opt</sub>*
# elif *constant-expression new-line group<sub>opt</sub>*
检查控制常量表达式的计算结果是否为非零。
4 Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the
defined
unary operator), just as in normal text. If the tokendefined
is generated as a result of this replacement process or use of thedefined
unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined.