views:

499

answers:

3

In C++, is this:

#ifdef COND_A && COND_B

the same as:

#if defined(COND_A) && defined(COND_B)

?

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

+8  A: 

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.

Evan Teran
Thanks for checking. I'm using Microsoft's compiler and it seems to allow it, but it just didn't seem right to me.
criddell
+6  A: 

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
Svetlozar Angelov
While what you say is correct, this does not answer the question at all, he asked if the two are the same...they are not.
Evan Teran
Svetlozar Angelov
A: 

how about #ifdef COND_A && defined(COND_B)?... does it work?...

FearNone
If you have a question, feel free to ask it using the "Ask Question" link at the top of every page. To answer this question, no, it doesn't work. `#if[n]def` takes exactly one identifier as its argument.
James McNellis