tags:

views:

89

answers:

3

A preprocessor definition that includes defined(X) will never evaluate to true, but (defined X) will. This occurs in MSVC9; I have not tested other preprocessors. A simple example:

#define FEATURE0 1
#define FEATURE1 0
#define FEATURE2 1

#define FEATURE3 (FEATURE0 && !FEATURE1 && (defined(FEATURE2)))
#define FEATURE4 (FEATURE0 && !FEATURE1 && (defined FEATURE2))
#define FEATURE5 (FEATURE0 && !FEATURE1 && (defined (FEATURE2)))

#if FEATURE3
#pragma message("FEATURE3 Enabled")
#elif (FEATURE0 && !FEATURE1 && (defined(FEATURE2)))
#pragma message("FEATURE3 Enabled (Fallback)")
#endif

#if FEATURE4
#pragma message("FEATURE4 Enabled")
#elif (FEATURE0 && !FEATURE1 && (defined FEATURE2))
#pragma message("FEATURE4 Enabled (Fallback)")
#endif

#if FEATURE5
#pragma message("FEATURE5 Enabled")
#elif (FEATURE0 && !FEATURE1 && (defined (FEATURE2)))
#pragma message("FEATURE5 Enabled (Fallback)")
#endif

The output from the compiler is:

1>FEATURE3 Enabled (Fallback)
1>FEATURE4 Enabled
1>FEATURE5 Enabled

Working cases: defined (X), defined( X ), and defined X.
Broken case: defined(X)

Why is defined evaluated differently when part of a definition, as in the #if cases in the example, compared to direct evaluation, as in the #elif cases in the example?

+5  A: 

defined is specific to #if and #elif. When using it through macro expansion the behavior is undefined.

mkj
This is the right answer and explicitly specified by 16.1/4 in the C++03 standard.
Roger Pate
+3  A: 

Remember that defined(X) isn't interpreted like a function call (a la sizeof(X)), it's parsed by a special language parser. This parser recognizes defined as a modifier to an if statement, not as an independent entity. When you are using defined(FEATURE2), it is not treating defined as a keyword but instead as a regular object or #defined entity (which doesn't exist in this case, causing your error). Later, when you use it inside the elif statement, the parser treats it as a proper keyword.

To be honest, I wasn't aware that defined(SOMETHING) would work in any case.

bta
+1  A: 

According to 6.10.1.3 in the C99 spec, the keyword defined is only recognized if it appears directly in the expression of the #if before any macro expansion in that expression. If macro expansion results in the keyword defined, the results are undefined.

Chris Dodd