Preprocessor problem or C++ compliant ?

Edward Diener eldlistmailingz@tropicsoft.com
Wed Aug 31 16:18:00 GMT 2011


Attempting to compile this small example program with gcc, with 
SOME_MACRO defined on the command line:

#if !defined(SOME_MACRO)
#define MACRO_1() 1
#else
#define MACRO_1()
#endif

#if defined(SOME_MACRO)
int main() { return 0; }
#elif MACRO_1() == 1
int main() { return 1; }
#endif

gives the error:

eliftest.cpp:9:17: error: operator '==' has no left operand

     "g++"  -ftemplate-depth-128 -O0 -fno-inline -Wall -pedantic -g 
-Wno-variadic-macros -DSOME_MACRO -c "eliftest.cpp"

If SOME_MACRO is defined, it seems to me that gcc should not be 
expanding the expression 'MACRO_1() == 1' and reporting an error.

If the program above is:

#if !defined(SOME_MACRO)
#define MACRO_1() 1
#else
#define MACRO_1()
#endif

#if defined(SOME_MACRO)
int main() { return 0; }
#else
#if MACRO_1() == 1
int main() { return 1; }
#endif
#endif

no compiler error is reported. This suggests that #elif is not the 
shorthand equivalent to #else-#if-#endif that one would expect in C++.



More information about the Gcc-help mailing list