Jens
Jens

Reputation: 72746

Rescanning the 'defined' operator after macro expansion: should it work?

Consider

#define FOOBAR (defined(FOO) || defined(BAR))

#if FOOBAR
/* Do stuff. */
#endif

Should this work? I'm asking because apparently my compilers have no problem with it, but the doxygen internal preprocessor thinks there is a syntax error with the #if. I know I could work around this with

#if defined(FOO) || defined(BAR)
#define FOOBAR 1
#endif
#if FOOBAR
/* Do stuff. */
#endif

Upvotes: 4

Views: 430

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126526

From the C99 spec:

6.10.1.3

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.

So if you use a macro that expands to defined as you have done, the result is undefined.

As with most undefined things in the C spec, its undefined because implementations prior to the standard handled it differently.

Upvotes: 5

Eran Zimmerman Gonen
Eran Zimmerman Gonen

Reputation: 4507

This sounds like a compiler-specific question. As long as you're just using this compiler, just try it out - put some code in the /*do stuff*/ part, and see whether or not the code is compiled.

Upvotes: 0

Related Questions