Boolean in ifdef: is "#ifdef A && B" the same as "#if defined(A) && defined(B)"?


Question

In C++, is this:

#ifdef A && B

the same as:

#if defined(A) && defined(B)

?

I was thinking it wasn't, but I haven't been able to find a difference with my compiler (VS2005).

1
76
6/6/2016 10:28:41 AM

Accepted Answer

They are not the same. The first one doesn't work (I tested in gcc 4.4.1). Error message was:

test.cc:1:15: warning: extra tokens at end of #ifdef directive

If you want to check if multiple things are defined, use the second one.

81
8/21/2009 2:09:30 PM

Conditional Compilation

You can use the defined operator in the #if directive to use expressions that evaluate to 0 or 1 within a preprocessor line. This saves you from using nested preprocessing directives. The parentheses around the identifier are optional. For example:

#if defined (MAX) && ! defined (MIN)  

Without using the defined operator, you would have to include the following two directives to perform the above example:

#ifdef max 
#ifndef min

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon