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).
The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined. It's equivalent to #if 0 when identifier hasn't been defined, or has been undefined by the #undef directive.
The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present. Syntax: #ifdef MACRO.
If an #ifdef is evaluated to be false, then the #elseif directive is processed as if it was an #ifdef directive. If all #elseif directives evaluate to be false, the #else directive is processed if it exists. Input is evaluated until an #endif directive is encountered.
#ifdef means if defined. If the symbol following #ifdef is defined, either using #define in prior source code or using a compiler command-line argument, the text up to the enclosing #endif is included by the preprocessor and therefore compiled. #if works similarly, but it evaluates the boolean expression following it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With