Preprocessor problem or C++ compliant ?

Ian Lance Taylor iant@google.com
Wed Aug 31 19:07:00 GMT 2011


Edward Diener <eldlistmailingz@tropicsoft.com> writes:

> 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.

gcc's behaviour matches my reading of the C99 standard.  The standard
says that each #if/#elif condition is checked.  It doesn't say that the
compiler should stop checking conditions after it finds one that is
true.  Instead, it says that the first group which is true is processed.

Ian



More information about the Gcc-help mailing list