可以为#define 分配defined 的结果吗?
Can a #define be assigned the result of defined?
下面代码的结果是用C还是C++定义的?
#define FOO
#define BAR defined(FOO)
#if BAR
int x = 1;
#else
int x = 2;
#endif
使用defined
作为条件指令的扩展被认为是未定义的行为。
- 来自 C99 标准:6.10.1/4 | C++11 Standard Last Working Draft(n4296) 16.1/4
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.
- 来自 C99 标准:6.10.8/4 | C++11 Standard Last Working Draft(n4296) 16.8/4
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还是C++定义的?
#define FOO
#define BAR defined(FOO)
#if BAR
int x = 1;
#else
int x = 2;
#endif
使用defined
作为条件指令的扩展被认为是未定义的行为。
- 来自 C99 标准:6.10.1/4 | C++11 Standard Last Working Draft(n4296) 16.1/4
If the token
defined
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.
- 来自 C99 标准:6.10.8/4 | C++11 Standard Last Working Draft(n4296) 16.8/4
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.